public function processInPieces(string $countQuery, string $query, $callback = null, string $key = null, int $bulkSize = 5000)
    {
        /** @var Database $database */
        $database = $this->framework->createInstance(Database::class);
        $total = $database->execute($countQuery);

        if ($total->total < 1) {
            return false;
        }

        $bulkSize = (int) $bulkSize;
        $totalCount = $total->total;
        $cycles = $totalCount / $bulkSize;

        for ($i = 0; $i <= $cycles; ++$i) {
            $result = $database->prepare($query)->limit($bulkSize, $i * $bulkSize)->execute();

            if ($result->numRows < 1) {
                return false;
            }

            if (\is_callable($callback)) {
                $return = [];

                while (false !== ($row = $result->fetchAssoc())) {
                    if ($key) {
                        if (isset($row[$key])) {
                            $return[$row[$key]] = $row;
                        }

                        continue;
                    }

                    $return[] = $row;
                }

                \call_user_func_array($callback, [$return]);
            }
        }

        return $totalCount;
    }