public static function copy($from, $to, $overwrite = true)
    {
        if (!file_exists($from)) {
            throw PathException::createUsingFormat(
                'The path "%s" does not exist.',
                $from
            );
        }

        $count = 0;

        if (is_file($from)) {
            if (file_exists($to) && !$overwrite) {
                return $count;
            }

            if (!@copy($from, $to)) {
                throw PathException::createUsingLastError();
            }

            $count++;
        } else {
            if (!file_exists($to) && !@mkdir($to)) {
                throw PathException::createUsingLastError();
            }

            if (!($handle = @opendir($from))) {
                throw PathException::createUsingLastError();
            }

            $empty = true;

            while (false !== ($entry = readdir($handle))) {
                if (('.' === $entry) || ('..' === $entry)) {
                    continue;
                }

                $empty = false;

                $count += self::copy(
                    $from . DIRECTORY_SEPARATOR . $entry,
                    $to . DIRECTORY_SEPARATOR . $entry,
                    $overwrite
                );
            }

            if ($empty) {
                $count++;
            }

            closedir($handle);
        }

        return $count;
    }