Tools   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 147
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 147
rs 10
c 0
b 0
f 0
wmc 12
lcom 1
cbo 3

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 21 3
A _checkExistance() 0 8 2
B uninstall() 0 47 6
A existance() 0 4 1
1
<?php
2
3
/**
4
 * Provides a layer for check / install / uninstall a plugin or theme
5
 *
6
 * PHP Version 5
7
 *
8
 * @category  Core
9
 * @package   Market
10
 * @author    Micha Schultz <[email protected]>
11
 * @copyright 2014 cSphere Team
12
 * @license   http://opensource.org/licenses/bsd-license Simplified BSD License
13
 * @link      http://www.csphere.eu
14
 **/
15
16
namespace csphere\core\market;
17
18
/**
19
 * Provides a layer for checkups to install and uninstall plugins
20
 *
21
 * @category  Core
22
 * @package   Market
23
 * @author    Micha Schultz <[email protected]>
24
 * @copyright 2014 cSphere Team
25
 * @license   http://opensource.org/licenses/bsd-license Simplified BSD License
26
 * @link      http://www.csphere.eu
27
 **/
28
29
class Tools
30
{
31
    /**
32
     * Short that will be used
33
     **/
34
    private $_short = '';
35
36
    /**
37
     * Type that will be used
38
     **/
39
    private $_type = '';
40
41
    /**
42
     * Local path
43
     **/
44
    private $_path = '';
45
46
    /**
47
     * Info xml file that will be used
48
     **/
49
    private $_file = '';
50
51
    /**
52
     * Remember positive existance checks
53
     **/
54
    private $_existance = false;
55
56
    /**
57
     * Array of possible errors
58
     **/
59
    private $_error = [];
60
61
    /**
62
     * Get plugin for install / update / delete it
63
     *
64
     * @param string $short Name of plugin or theme
65
     * @param string $type  Type of input data
66
     *
67
     * @throws \Exception
68
     *
69
     * @return \csphere\core\market\Tools
70
     **/
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
71
72
    public function __construct($short, $type)
73
    {
74
75
        $this->_path = \csphere\core\init\path();
76
77
        $this->_type = $type;
78
79
        $this->_short = $short;
80
81
        $this->_file = $this->_path . 'csphere/' . $this->_type . 's'
82
                     . '/' . $this->_short . '/' . $this->_type . '.xml';
83
84
        //Check correct type
85
        if ($this->_type != 'plugin' && $this->_type != 'theme') {
86
87
            throw new \Exception('Only plugin oder theme is allowed as type');
88
        }
89
90
        // Check plugin / theme existence
91
        $this->_checkExistance();
92
    }
93
94
    /**
95
     * Content of a directory as an array
96
     *
97
     * @return void
98
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
99
100
    private function _checkExistance()
101
    {
102
        // Check for xml file
103
        if (file_exists($this->_file)) {
104
105
            $this->_existance = true;
106
        }
107
    }
108
109
    /**
110
     * Method to uninstall plugin with pre check
111
     *
112
     * @param boolean $action Perform uninstall after pre check, if true
113
     *
114
     * @return boolean
115
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
116
117
    public function uninstall($action = false)
118
    {
119
        if (!$this->_existance) {
120
121
            array_push($this->_error, 'Sample Error');
122
123
            return false;
124
        }
125
126
        // Check plugin dependencies
127
        $loader = \csphere\core\service\Locator::get();
128
        $xml = $loader->load('xml', $this->_type);
129
        $data = $xml->source($this->_type, $this->_short);
130
131
        $vendor = $data['vendor'];
132
133
        // Load plugin list
134
        $meta = new \csphere\core\plugins\Metadata();
135
        $plugins = $meta->details();
136
137
        foreach ($plugins as $plugin) {
138
139
            // Load plugin xml infos
140
            $data = $xml->source('plugin', $plugin['short']);
141
142
            $dependencies = $data['environment'][0]['needed'];
143
144
            foreach ($dependencies as $dependency) {
145
146
                $control_plugin = $vendor . '.' . $this->_short;
147
                $check_plugin = $dependency['vendor'] . '.' . $dependency['plugin'];
148
149
                if ($control_plugin == $check_plugin) {
150
151
                    // Dependencies found, no uninstall
152
                    return false;
153
                }
154
            }
155
        }
156
157
        if ($action) {
158
159
            //TODO Perform uninstall
160
        }
161
162
        return true;
163
    }
164
165
    /**
166
     * Checks if plugin oder theme exists
167
     *
168
     * @return boolean
169
     **/
0 ignored issues
show
Coding Style introduced by
There must be no blank lines after the function comment
Loading history...
170
171
    public function existance()
172
    {
173
        return $this->_existance;
174
    }
175
}
176