Passed
Push — master ( 7d2ca3...cd3420 )
by Jared
03:34 queued 01:46
created

Module.php (6 issues)

Labels
Severity
1
<?php
2
3
namespace AnyCloud;
4
5
use AnyCloud\Form\Configform;
6
use Omeka\Module\AbstractModule;
1 ignored issue
show
The type Omeka\Module\AbstractModule was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Omeka\Module\Exception\ModuleCannotInstallException;
1 ignored issue
show
The type Omeka\Module\Exception\M...eCannotInstallException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Zend\Mvc\Controller\AbstractController;
1 ignored issue
show
The type Zend\Mvc\Controller\AbstractController was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use Zend\Mvc\MvcEvent;
1 ignored issue
show
The type Zend\Mvc\MvcEvent was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use Zend\ServiceManager\ServiceLocatorInterface;
1 ignored issue
show
The type Zend\ServiceManager\ServiceLocatorInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Zend\View\Renderer\PhpRenderer;
1 ignored issue
show
The type Zend\View\Renderer\PhpRenderer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
13
class Module extends AbstractModule
14
{
15
    public $adapter;
16
17
    public function getConfig()
18
    {
19
        return include __DIR__.'/config/module.config.php';
20
    }
21
22
    public function onBootstrap(MvcEvent $event)
23
    {
24
        parent::onBootstrap($event);
25
26
        $this->setFileStoreAlias();
27
        require __DIR__.'/vendor/autoload.php'; // Add autoloader for module-specific requirements
28
    }
29
30
    /**
31
     * Generate user messages in case of install problems.
32
     *
33
     * @param ServiceLocatorInterface $serviceLocator
34
     */
35
    public function install(ServiceLocatorInterface $serviceLocator)
36
    {
37
        if (!file_exists(__DIR__.'/vendor/autoload.php')) {
38
            throw new ModuleCannotInstallException('The Any Cloud components via composer should be installed. See module’s installation documentation.');
39
        }
40
        $settings = $serviceLocator->get('Omeka\Settings');
41
        $this->manageSettings($settings, 'install');
42
    }
43
44
    /**
45
     * Uninstall module and settings.
46
     *
47
     * @param ServiceLocatorInterface $serviceLocator
48
     */
49
    public function uninstall(ServiceLocatorInterface $serviceLocator)
50
    {
51
        $settings = $serviceLocator->get('Omeka\Settings');
52
        $this->manageSettings($settings, 'uninstall');
53
    }
54
55
    /**
56
     * Script to run when upgrading module.
57
     *
58
     * @param string                  $oldVersion
59
     * @param string                  $newVersion
60
     * @param ServiceLocatorInterface $serviceLocator
61
     */
62
    public function upgrade($oldVersion, $newVersion, ServiceLocatorInterface $serviceLocator)
63
    {
64
        require_once 'data/scripts/upgrade.php';
65
    }
66
67
    public function getConfigForm(PhpRenderer $renderer)
68
    {
69
        $services = $this->getServiceLocator();
70
        $config = $services->get('Config');
71
        $settings = $services->get('Omeka\Settings');
72
        $form = $services->get('FormElementManager')->get(ConfigForm::class);
73
        $data = [];
74
        $defaultSettings = $config[strtolower(__NAMESPACE__)]['config'];
75
        foreach ($defaultSettings as $name => $value) {
76
            $data[$name] = $settings->get($name, $value);
77
        }
78
        $form->init();
79
        $form->setData($data);
80
        $html = $renderer->render('anycloud/module/config', [
81
            'form' => $form,
82
        ]);
83
84
        return $html;
85
    }
86
87
    public function handleConfigForm(AbstractController $controller)
88
    {
89
        $serviceLocator = $this->getServiceLocator();
90
        $settings = $serviceLocator->get('Omeka\Settings');
91
        $form = $serviceLocator->get('FormElementManager')->get(ConfigForm::class);
92
        $params = $controller->getRequest()->getPost();
93
        $form->init();
94
        $form->setData($params);
95
        if (!$form->isValid()) {
96
            $controller->messenger()->addErrors($form->getMessages());
97
98
            return false;
99
        }
100
        $params = $form->getData();
101
        $defaultSettings = $this->getDefaultSettings();
102
        $params = array_intersect_key($params, $defaultSettings);
103
        foreach ($params as $name => $value) {
104
            $settings->set($name, $value);
105
        }
106
    }
107
108
    /**
109
     * Manage module settings.
110
     *
111
     * @param ServiceLocatorInterface $settings Object containing Omeka settings
112
     * @param string                  $process  Process used to manage setting (`install` or `uninstall`)
113
     * @param string                  $key      Name of $settings key to manage
114
     */
115
    protected function manageSettings($settings, $process, $key = 'config')
116
    {
117
        $config = require __DIR__.'/config/module.config.php';
118
        $defaultSettings = $config[strtolower(__NAMESPACE__)][$key];
119
        foreach ($defaultSettings as $name => $value) {
120
            switch ($process) {
121
                case 'install':
122
                    $settings->set($name, $value);
123
                    break;
124
                case 'uninstall':
125
                    $settings->delete($name);
126
                    break;
127
            }
128
        }
129
    }
130
131
    /**
132
     * Override default file store alias to use Any Cloud module for uploads instead.
133
     */
134
    private function setFileStoreAlias()
135
    {
136
        $serviceLocator = $this->getServiceLocator();
137
        $settings = $serviceLocator->get('Omeka\Settings');
138
        $this->adapter = $settings->get('anycloud_adapter');
139
        if (isset($this->adapter) && $this->adapter !== 'default') {
140
            $serviceLocator->setAlias('Omeka\File\Store', File\Store\AnyCloud::class);
141
        }
142
    }
143
144
    private function getDefaultSettings($key = 'config')
145
    {
146
        $serviceLocator = $this->getServiceLocator();
147
        // TODO Fix so that configs are actually grabbed and the module can be deleted if desired
148
        $config = $serviceLocator->get('Config');
149
150
        return $config[strtolower(__NAMESPACE__)][$key];
151
    }
152
}
153