<?php

namespace Aps\App\Joomla;

require_once __DIR__ . '/bootstrap.php';

/**
 * Simulate com_joomlaupdate component - process native update model
 *
 * administrator/components/com_joomlaupdate/models/default.php
 * \JoomlaupdateControllerUpdate::finalise
 * \JoomlaupdateModelDefault::finaliseUpgrade
 * \JoomlaInstallerScript::update
 */
class Upgrader
{
    protected function deleteOldFolders()
    {
        $old_folders = array(
            '/libraries/cms/application',
            '/libraries/cms/authentication',
            '/libraries/cms/captcha',
            '/libraries/cms/component/exception',
            '/libraries/cms/component/router/rules',
            '/libraries/cms/component/router',
            '/libraries/cms/component',
            '/libraries/cms/editor',
            '/libraries/cms/error',
            '/libraries/cms/extension',
            '/libraries/cms/form/field',
            '/libraries/cms/form/rule',
            '/libraries/cms/form',
            '/libraries/cms/help',
            '/libraries/cms/helper',
            '/libraries/cms/installer/adapter',
            '/libraries/cms/installer/manifest',
            '/libraries/cms/installer',
            '/libraries/cms/language',
            '/libraries/cms/layout',
            '/libraries/cms/library',
            '/libraries/cms/menu',
            '/libraries/cms/module',
            '/libraries/cms/pagination',
            '/libraries/cms/pathway',
            '/libraries/cms/plugin',
            '/libraries/cms/response',
            '/libraries/cms/router',
            '/libraries/cms/schema/changeitem',
            '/libraries/cms/schema',
            '/libraries/cms/search',
            '/libraries/cms/table',
            '/libraries/cms/toolbar/button',
            '/libraries/cms/toolbar',
            '/libraries/cms/ucm',
            '/libraries/cms/version',
        );

        foreach ($old_folders as $folder) {
            if (\JFolder::exists(JPATH_ROOT . $folder) && !\JFolder::delete(JPATH_ROOT . $folder)) {
                echo \JText::sprintf('FILES_JOOMLA_ERROR_FILE_FOLDER', $folder), PHP_EOL;
            }
        }
    }

    public function execute($version, $release)
    {
        jimport('joomla.filesystem.folder');
        if (file_exists(JPATH_INSTALLATION)) {
            \JFolder::delete(JPATH_INSTALLATION);
        }
        $this->deleteOldFolders();

        // pseudo session a specially for \JoomlaInstallerScript::flushSessions
        \JFactory::$session = new \JSessionAps;

        // \JModelLegacy::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_joomlaupdate/models', 'JoomlaupdateModel');
        //
        // /** @var \JoomlaupdateModelDefault $model */
        // $model = \JModelLegacy::getInstance('default', 'JoomlaupdateModel');
        // \$model->finaliseUpgrade();

        require_once JPATH_ADMINISTRATOR . '/components/com_joomlaupdate/models/default.php';
        $model = new \JoomlaupdateModelDefault;
        $model->finaliseUpgrade();
    }
}
