Cli::createCli()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 0
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\controllers;
11
12
use gplcart\core\CliController;
13
use gplcart\core\models\Store as StoreModel;
14
use gplcart\modules\demo\models\Demo as DemoModuleModel;
15
16
/**
17
 * Handles Demo module CLI commands
18
 */
19
class Cli extends CliController
20
{
21
22
    /**
23
     * Demo module model instance
24
     * @var \gplcart\modules\demo\models\Demo $demo
25
     */
26
    protected $demo;
27
28
    /**
29
     * Store model class instance
30
     * @var \gplcart\modules\demo\models\Store $store
31
     */
32
    protected $store;
33
34
    /**
35
     * The current demo handler id
36
     * @var string
37
     */
38
    protected $data_handler_id;
39
40
    /**
41
     * The current demo store ID
42
     * @var integer
43
     */
44
    protected $data_store_id;
45
46
    /**
47
     * @param StoreModel $store
48
     * @param DemoModuleModel $demo
49
     */
50
    public function __construct(StoreModel $store, DemoModuleModel $demo)
51
    {
52
        parent::__construct();
53
54
        $this->demo = $demo;
55
        $this->store = $store;
0 ignored issues
show
Documentation Bug introduced by
It seems like $store of type object<gplcart\core\models\Store> is incompatible with the declared type object<gplcart\modules\demo\models\Store> of property $store.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
56
57
        $this->data_store_id = $this->getArgument('store', 1);
58
        $this->data_handler_id = $this->getArgument('package', 'default');
59
    }
60
61
    /**
62
     * Handles "demo-create" command
63
     */
64
    public function createCli()
65
    {
66
        if ($this->validateCli()) {
67
            $result = $this->demo->create($this->data_handler_id, $this->data_store_id);
68
            if ($result !== true) {
69
                $this->setError('result', $result);
70
            }
71
        }
72
73
        $this->output();
74
    }
75
76
    /**
77
     * Handles "demo-delete" command
78
     */
79
    public function deleteCli()
80
    {
81
        if ($this->validateCli()) {
82
            $result = $this->demo->delete($this->data_handler_id, $this->data_store_id);
83
            if ($result !== true) {
84
                $this->setError('result', $result);
85
            }
86
        }
87
88
        $this->output();
89
    }
90
91
    /**
92
     * Validates submitted arguments
93
     * @return boolean
94
     */
95
    protected function validateCli()
96
    {
97
        if (!$this->store->get($this->data_store_id)) {
98
            $this->setError('store_id', $this->text('Store does not exist'));
99
        }
100
101
        if (!$this->demo->getHandler($this->data_handler_id)) {
102
            $this->setError('handler_id', $this->text('Unknown handler'));
103
        }
104
        return !$this->isError();
105
    }
106
107
}
108