Demo   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

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

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getHandlers() 0 22 2
A getHandler() 0 5 2
A create() 0 11 2
A delete() 0 11 2
A setCreatedEntityId() 0 6 1
A getCreatedEntityId() 0 4 1
A resetCreatedEntityId() 0 4 1
1
<?php
2
3
/**
4
 * @package Demo 
5
 * @author Iurii Makukh <[email protected]> 
6
 * @copyright Copyright (c) 2017, Iurii Makukh <[email protected]> 
7
 * @license https://www.gnu.org/licenses/gpl-3.0.en.html GPL-3.0+ 
8
 */
9
10
namespace gplcart\modules\demo\models;
11
12
use gplcart\core\Cache,
13
    gplcart\core\Model,
14
    gplcart\core\Handler;
15
use gplcart\core\models\Language as LanguageModel;
16
17
/**
18
 * Manages basic behaviors and data related to Demo module
19
 */
20
class Demo extends Model
21
{
22
23
    /**
24
     * Language model instance
25
     * @var \gplcart\core\models\Language $language
26
     */
27
    protected $language;
28
29
    /**
30
     * @param LanguageModel $language
31
     */
32
    public function __construct(LanguageModel $language)
33
    {
34
        parent::__construct();
35
36
        $this->language = $language;
37
    }
38
39
    /**
40
     * Returns an array of handlers
41
     */
42
    public function getHandlers()
43
    {
44
        $handlers = &Cache::memory(__METHOD__);
45
46
        if (isset($handlers)) {
47
            return $handlers;
48
        }
49
50
        $handlers = array();
51
52
        $handlers['default'] = array(
53
            'title' => $this->language->text('Default (watches)'),
54
            'description' => $this->language->text('Create basic demo set containing products, categories and banners'),
55
            'handlers' => array(
56
                'create' => array('gplcart\\modules\\demo\\handlers\\Demo', 'create'),
57
                'delete' => array('gplcart\\modules\\demo\\handlers\\Demo', 'delete')
58
            )
59
        );
60
61
        $this->hook->fire('module.demo.handlers', $handlers, $this);
62
        return $handlers;
63
    }
64
65
    /**
66
     * Returns a handler
67
     * @param string $handler_id
68
     * @return array
69
     */
70
    public function getHandler($handler_id)
71
    {
72
        $handlers = $this->getHandlers();
73
        return empty($handlers[$handler_id]) ? array() : $handlers[$handler_id];
74
    }
75
76
    /**
77
     * Create a demo content
78
     * @param string $handler_id
79
     * @param integer $store_id
80
     * @return string|integer
81
     */
82
    public function create($handler_id, $store_id)
83
    {
84
        $existing = $this->getCreatedEntityId($handler_id, $store_id);
85
86
        if (!empty($existing)) {
87
            return $this->language->text('Demo content already exists');
88
        }
89
90
        $handlers = $this->getHandlers();
91
        return Handler::call($handlers, $handler_id, 'create', array($store_id, $this));
92
    }
93
94
    /**
95
     * Deletes the demo content
96
     * @param string $handler_id
97
     * @param integer $store_id
98
     * @return string|integer
99
     */
100
    public function delete($handler_id, $store_id)
101
    {
102
        $existing = $this->getCreatedEntityId($handler_id, $store_id);
103
104
        if (empty($existing)) {
105
            return true;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return true; (boolean) is incompatible with the return type documented by gplcart\modules\demo\models\Demo::delete of type string|integer.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

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

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
106
        }
107
108
        $handlers = $this->getHandlers();
109
        return Handler::call($handlers, $handler_id, 'delete', array($store_id, $this));
110
    }
111
112
    /**
113
     * Set an array of created entity IDs in the database
114
     * @param string $handler_id
115
     * @param integer $store_id
116
     * @param array $data
117
     * @return boolean
118
     */
119
    public function setCreatedEntityId($handler_id, $store_id, array $data)
120
    {
121
        // Remove unneeded keys before saving
122
        $cleaned = array_map('array_values', $data);
123
        return $this->config->set("module_demo_{$handler_id}_{$store_id}", $cleaned);
124
    }
125
126
    /**
127
     * Returns an array of previously created entity IDs
128
     * @param string $handler_id
129
     * @param integer $store_id
130
     * @return array
131
     */
132
    public function getCreatedEntityId($handler_id, $store_id)
133
    {
134
        return $this->config->get("module_demo_{$handler_id}_{$store_id}", array());
135
    }
136
137
    /**
138
     * Removes all saved data about previously created entity IDs from the database
139
     * @param string $handler_id
140
     * @param integer $store_id
141
     * @return boolean
142
     */
143
    public function resetCreatedEntityId($handler_id, $store_id)
144
    {
145
        return $this->config->reset("module_demo_{$handler_id}_{$store_id}");
146
    }
147
148
}
149