1:   2:   3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13:  14:  15:  16:  17:  18:  19:  20:  21:  22:  23:  24:  25:  26:  27:  28:  29:  30:  31:  32:  33:  34:  35:  36:  37:  38:  39:  40:  41:  42:  43:  44:  45:  46:  47:  48:  49:  50:  51:  52:  53:  54:  55:  56:  57:  58:  59:  60:  61:  62:  63:  64:  65:  66:  67:  68:  69:  70:  71:  72:  73:  74:  75:  76:  77:  78:  79:  80:  81:  82:  83:  84:  85:  86:  87:  88:  89:  90:  91:  92:  93:  94:  95:  96:  97:  98:  99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 
<?php
namespace Omeka\Mvc;

use Composer\Semver\Comparator;
use Omeka\Module as OmekaModule;
use Zend\ServiceManager\ServiceLocatorInterface;

class Status
{
    /**
     * @var ServiceLocatorInterface
     */
    protected $serviceLocator;

    /**
     * @var bool
     */
    protected $isInstalled;

    /**
     * @var bool
     */
    protected $isApiRequest;

    public function __construct(ServiceLocatorInterface $serviceLocator)
    {
        $this->serviceLocator = $serviceLocator;
    }

    /**
     * Check whether Omeka is currently installed.
     *
     * @return bool
     */
    public function isInstalled()
    {
        return (bool) $this->isInstalled;
    }

    /**
     * Set whether Omeka is currently installed.
     *
     * Since it's invoked so early in the application's initialization, the
     * module manager is responsible for determining an installed state. The
     * heuristic is the existence of the module table.
     *
     * @param bool $isInstalled
     */
    public function setIsInstalled($isInstalled)
    {
        $this->isInstalled = (bool) $isInstalled;
    }

    /**
     * Check whether the current HTTP request is an API request.
     *
     * The heuristic for determining an API request is a route match against the
     * API controller.
     *
     * @return bool
     */
    public function isApiRequest()
    {
        if (null !== $this->isApiRequest) {
            return $this->isApiRequest;
        }
        // Get the route match.
        $router = $this->serviceLocator->get('Router');
        $request = $this->serviceLocator->get('Request');
        $routeMatch = $router->match($request);
        if (null === $routeMatch) {
            // No matching route; not an API request.
            return false;
        }
        $this->isApiRequest = 'Omeka\Controller\Api'
            === $routeMatch->getParam('controller');
        return $this->isApiRequest;
    }

    /**
     * Check whether Omeka needs a version update.
     *
     * An update is needed when the code version is more recent than the
     * installed version.
     *
     * @return bool
     */
    public function needsVersionUpdate()
    {
        return Comparator::greaterThan($this->getVersion(), $this->getInstalledVersion());
    }

    /**
     * Check whether Omeka needs a database migration.
     *
     * A migration is needed when there are un-performed migrations.
     *
     * @return bool
     */
    public function needsMigration()
    {
        $migrationManager = $this->serviceLocator->get('Omeka\MigrationManager');
        if ($migrationManager->getMigrationsToPerform()) {
            return true;
        }
        return false;
    }

    /**
     * Get the Omeka code version.
     *
     * @return string
     */
    public function getVersion()
    {
        return OmekaModule::VERSION;
    }

    /**
     * Get the Omeka installed version.
     *
     * @return string
     */
    public function getInstalledVersion()
    {
        return $this->serviceLocator->get('Omeka\Settings')->get('version');
    }
}