Completed
Push — master ( 71cae1...dce477 )
by Iurii
01:01
created

Install::getModuleModel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/**
4
 * @package Base
5
 * @author Iurii Makukh <[email protected]>
6
 * @copyright Copyright (c) 2015, Iurii Makukh
7
 * @license https://www.gnu.org/licenses/gpl.html GNU/GPLv3
8
 */
9
10
namespace gplcart\modules\base\models;
11
12
use gplcart\core\Container,
13
    gplcart\core\Module;
14
use gplcart\core\models\Translation as TranslationModel;
15
16
/**
17
 * Installer model
18
 */
19
class Install
20
{
21
    /**
22
     * Translation UI model instance
23
     * @var \gplcart\core\models\Translation $translation
24
     */
25
    protected $translation;
26
27
    /**
28
     * Module class instance
29
     * @var \gplcart\core\Module $module
30
     */
31
    protected $module;
32
33
    /**
34
     * @param Module $module
35
     * @param TranslationModel $translation
36
     */
37
    public function __construct(Module $module, TranslationModel $translation)
38
    {
39
        $this->module = $module;
40
        $this->translation = $translation;
41
    }
42
43
    /**
44
     * Returns an array of modules required for Base installation profile
45
     * @return array
46
     */
47
    public function getRequiredModules()
48
    {
49
        return gplcart_config_get(__DIR__ . '/../config/modules.php');
50
    }
51
52
    /**
53
     * Whether the current distribution contains all the required modules
54
     * @return bool
55
     */
56
    public function hasAllRequiredModules()
57
    {
58
        $required = $this->getRequiredModules();
59
        $available = array_keys($this->module->getList());
60
        $difference = array_diff($required, array_intersect($required, $available));
61
62
        return empty($difference);
63
    }
64
65
    /**
66
     * Install all required modules
67
     * @return bool
68
     */
69
    public function installModules()
70
    {
71
        foreach ($this->getRequiredModules() as $module_id) {
72
            if ($this->getModuleModel()->install($module_id) !== true) {
73
                return $this->translation->text('Failed to install module %module_id', array('%module_id' => $module_id));
74
            }
75
        }
76
77
        return true;
78
    }
79
80
    /**
81
     * Returns Module model class instance
82
     * @return \gplcart\core\models\Module
83
     */
84
    protected function getModuleModel()
85
    {
86
        return Container::get('gplcart\\core\\models\\Module');
87
    }
88
89
    /**
90
     * Create demo content using Demo module
91
     * @param integer $store_id
92
     * @param string $handler_id
93
     * @return bool|string
94
     */
95
    public function createDemo($store_id, $handler_id)
96
    {
97
        return $this->getDemoModule()->create($store_id, $handler_id);
98
    }
99
100
    /**
101
     * Returns an array of demo handlers
102
     * @return array
103
     */
104
    public function getDemoHandlers()
105
    {
106
        return $this->getDemoModule()->getHandlers();
107
    }
108
109
    /**
110
     * Returns Demo module instance
111
     * @return \gplcart\modules\demo\Demo
112
     * @throws \RuntimeException
113
     */
114
    public function getDemoModule()
115
    {
116
        $instance = $this->module->getInstance('demo');
117
118
        if ($instance instanceof \gplcart\modules\demo\Demo) {
0 ignored issues
show
Bug introduced by
The class gplcart\modules\demo\Demo does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
119
            return $instance;
120
        }
121
122
        throw new \RuntimeException('Failed to get instance of Demo module');
123
    }
124
125
}
126