Completed
Push — master ( 85914a...2ce878 )
by Harald
06:39 queued 03:01
created

PagesAbstract::actionExists()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 20
Code Lines 11

Duplication

Lines 5
Ratio 25 %

Importance

Changes 0
Metric Value
cc 4
eloc 11
nc 6
nop 1
dl 5
loc 20
rs 9.2
c 0
b 0
f 0
1
<?php
2
/**
3
  * osCommerce Online Merchant
4
  *
5
  * @copyright Copyright (c) 2015 osCommerce; http://www.oscommerce.com
6
  * @license GPL; http://www.oscommerce.com/gpllicense.txt
7
  */
8
9
namespace OSC\OM;
10
11
use OSC\OM\HTML;
12
use OSC\OM\OSCOM;
13
use OSC\OM\Registry;
14
15
abstract class PagesAbstract implements \OSC\OM\PagesInterface
16
{
17
    public $data = [];
18
19
    protected $code;
20
    protected $file = 'main.php';
21
    protected $use_site_template = true;
22
    protected $site;
23
    protected $actions_run = [];
24
    protected $ignored_actions = [];
25
    protected $is_rpc = false;
26
27
    protected $app;
28
29
    final public function __construct(\OSC\OM\SitesInterface $site)
30
    {
31
        $this->code = (new \ReflectionClass($this))->getShortName();
32
        $this->site = $site;
33
34
        $this->init();
35
    }
36
37
    protected function init()
38
    {
39
    }
40
41
    public function getCode()
42
    {
43
        return $this->code;
44
    }
45
46
    public function getFile()
47
    {
48
        if (isset($this->file)) {
49
            return dirname(OSCOM::BASE_DIR) . '/' . str_replace('\\', '/', (new \ReflectionClass($this))->getNamespaceName()) . '/templates/' . $this->file;
50
        }
51
    }
52
53
    public function useSiteTemplate()
54
    {
55
        return $this->use_site_template;
56
    }
57
58
    public function setFile($file)
59
    {
60
        $this->file = $file;
61
    }
62
63
    public function isActionRequest()
64
    {
65
        $furious_pete = [];
66
67 View Code Duplication
        if (count($_GET) > $this->site->actions_index) {
0 ignored issues
show
Bug introduced by
Accessing actions_index on the interface OSC\OM\SitesInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
68
            $furious_pete = array_keys(array_slice($_GET, $this->site->actions_index, null, true));
0 ignored issues
show
Bug introduced by
Accessing actions_index on the interface OSC\OM\SitesInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
69
        }
70
71
        if (!empty($furious_pete)) {
72
            $action = HTML::sanitize(basename($furious_pete[0]));
73
74
            if (!in_array($action, $this->ignored_actions) && $this->actionExists($action)) {
75
                return true;
76
            }
77
        }
78
79
        return false;
80
    }
81
82
    public function runAction($actions)
83
    {
84
        if (!is_array($actions)) {
85
            $actions = [
86
                $actions
87
            ];
88
        }
89
90
        $run = [];
91
92
        foreach ($actions as $action) {
93
            $run[] = $action;
94
95
            if ($this->actionExists($run)) {
96
                $this->actions_run[] = $action;
97
98
                $class = $this->getActionClassName($run);
99
100
                $ns = explode('\\', $class);
101
102
                if ((count($ns) > 2) && ($ns[0] == 'OSC') && ($ns[1] == 'Apps')) {
103
                    if (isset($this->app) && is_subclass_of($this->app, 'OSC\OM\AppAbstract')) {
104
                        if ($this->app->definitionsExist(implode('/', array_slice($ns, 4)))) {
105
                            $this->app->loadDefinitions(implode('/', array_slice($ns, 4)));
106
                        }
107
                    }
108
                }
109
110
                $action = new $class($this);
111
112
                $action->execute();
113
114
                if ($action->isRPC()) {
115
                    $this->is_rpc = true;
116
                }
117
            } else {
118
                break;
119
            }
120
        }
121
    }
122
123
    public function runActions()
124
    {
125
        $actions = $furious_pete = [];
126
127 View Code Duplication
        if (count($_GET) > $this->site->actions_index) {
0 ignored issues
show
Bug introduced by
Accessing actions_index on the interface OSC\OM\SitesInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
128
            $furious_pete = array_keys(array_slice($_GET, $this->site->actions_index, null, true));
0 ignored issues
show
Bug introduced by
Accessing actions_index on the interface OSC\OM\SitesInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
129
        }
130
131
        foreach ($furious_pete as $action) {
132
            $action = HTML::sanitize(basename($action));
133
134
            $actions[] = $action;
135
136
            if (in_array($action, $this->ignored_actions) || !$this->actionExists($actions)) {
137
                array_pop($actions);
138
139
                break;
140
            }
141
        }
142
143
        if (!empty($actions)) {
144
            $this->runAction($actions);
145
        }
146
    }
147
148
    public function actionExists($action)
149
    {
150
        if (!is_array($action)) {
151
            $action = [
152
                $action
153
            ];
154
        }
155
156
        $class = $this->getActionClassName($action);
157
158
        if (class_exists($class)) {
159 View Code Duplication
            if (is_subclass_of($class, 'OSC\OM\PagesActionsInterface')) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of returns inconsistent results on some PHP versions for interfaces; you could instead use ReflectionClass::implementsInterface.
Loading history...
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
160
                return true;
161
            } else {
162
                trigger_error('OSC\OM\PagesAbstract::actionExists() - ' . implode('\\', $action) . ': Action does not implement OSC\OM\PagesActionInterface and cannot be loaded.');
163
            }
164
        }
165
166
        return false;
167
    }
168
169
    public function getActionsRun()
170
    {
171
        return $this->actions_run;
172
    }
173
174
    public function isRPC()
175
    {
176
        return ($this->is_rpc === true);
177
    }
178
179
    protected function getActionClassName($action)
180
    {
181
        if (!is_array($action)) {
182
            $action = [
183
                $action
184
            ];
185
        }
186
187
        return (new \ReflectionClass($this))->getNamespaceName() . '\\Actions\\' . implode('\\', $action);
188
    }
189
}
190