Completed
Push — 3.0 ( fea5eb...c29f0e )
by Vermeulen
02:17
created

CtrlRouterLink::obtainCtrlRouterLinkTasks()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.6666
c 0
b 0
f 0
cc 2
nc 1
nop 0
1
<?php
2
3
namespace BFW\Core\AppSystems;
4
5
class CtrlRouterLink extends AbstractSystem
6
{
7
    /**
8
     * @var \stdClass|null $ctrlRouterInfos Infos from router for controller
9
     * system
10
     */
11
    protected $ctrlRouterInfos;
12
    
13
    /**
14
     * {@inheritdoc}
15
     * 
16
     * @return \stdClass|null
17
     */
18
    public function __invoke()
19
    {
20
        return $this->ctrlRouterInfos;
21
    }
22
    
23
    /**
24
     * Getter accessor for property ctrlRouterInfos
25
     * 
26
     * @return \stdClass|null
27
     */
28
    public function getCtrlRouterInfos()
29
    {
30
        return $this->ctrlRouterInfos;
31
    }
32
    
33
    /**
34
     * {@inheritdoc}
35
     * Initialize the ctrlRouterInfos property
36
     * Create the new runTasks ctrlRouterLink, add him to subjectList and send
37
     * the notify to inform the adding.
38
     */
39
    public function init()
40
    {
41
        //Others properties can be dynamically added by modules
42
        $this->ctrlRouterInfos = (object) [
43
            'isFound' => false,
44
            'forWho'  => null,
45
            'target'  => null,
46
            'datas'   => null
47
        ];
48
        
49
        $ctrlRouterTask = new \BFW\RunTasks(
50
            $this->obtainCtrlRouterLinkTasks(),
51
            'ctrlRouterLink'
52
        );
53
        
54
        $subjectList = \BFW\Application::getInstance()->getSubjectList();
0 ignored issues
show
Documentation Bug introduced by
The method getSubjectList does not exist on object<BFW\Application>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
55
        $subjectList->addSubject($ctrlRouterTask, 'ctrlRouterLink');
56
        
57
        $runTasks = $subjectList->getSubjectByName('ApplicationTasks');
58
        $runTasks->sendNotify('bfw_ctrlRouterLink_subject_added');
59
        
60
        $this->initStatus = true;
61
    }
62
    
63
    /**
64
     * List all tasks runned by ctrlRouterLink
65
     * 
66
     * @return array
67
     */
68
    protected function obtainCtrlRouterLinkTasks()
69
    {
70
        return [
71
            'searchRoute'     => (object) [
72
                'context' => $this->ctrlRouterInfos
73
            ],
74
            'checkRouteFound' => (object) [
75
                'callback' => function() {
76
                    if ($this->ctrlRouterInfos->isFound === false) {
77
                        http_response_code(404);
78
                    }
79
                }
80
            ],
81
            'execRoute'       => (object) [
82
                'context' => $this->ctrlRouterInfos
83
            ]
84
        ];
85
    }
86
    
87
    /**
88
     * {@inheritdoc}
89
     */
90
    public function toRun()
91
    {
92
        return true;
93
    }
94
    
95
    /**
96
     * {@inheritdoc}
97
     * Execute the ctrlRouter tasks
98
     */
99
    public function run()
100
    {
101
        $this->runCtrlRouterLink();
102
        $this->runStatus = true;
103
    }
104
    
105
    /**
106
     * Execute the ctrlRouter task to find the route and the controller.
107
     * If nothing is found (context object), return an 404 error.
108
     * Not executed in cli.
109
     * 
110
     * @return void
111
     */
112
    protected function runCtrlRouterLink()
113
    {
114
        if (PHP_SAPI === 'cli') {
115
            return;
116
        }
117
        
118
        \BFW\Application::getInstance()
0 ignored issues
show
Documentation Bug introduced by
The method getSubjectList does not exist on object<BFW\Application>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
119
            ->getSubjectList()
120
            ->getSubjectByName('ctrlRouterLink')
121
            ->run();
122
    }
123
}
124