Passed
Push — master ( 8b88c7...8cc771 )
by Alex
03:01
created

ServiceBase::initTransport()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Mezon\Service;
3
4
use Mezon\Security\ProviderInterface;
5
use Mezon\Service\ServiceRestTransport\ServiceRestTransport;
6
7
/**
8
 * Class Service
9
 *
10
 * @package Mezon
11
 * @subpackage ServiceBase
12
 * @author Dodonov A.A.
13
 * @version v.1.0 (2019/12/09)
14
 * @copyright Copyright (c) 2019, aeon.org
15
 */
16
17
/**
18
 * Base service class
19
 *
20
 * It bounds together transport, request parameters fetcher, logic, authorization and model
21
 *
22
 * @author Dodonov A.A.
23
 */
24
class ServiceBase
25
{
26
27
    /**
28
     * Service's ransport
29
     *
30
     * @var object Service transport object
31
     */
32
    private $serviceTransport;
33
34
    /**
35
     * Constructor
36
     *
37
     * @param TransportInterface $serviceTransport
38
     *            service's transport
39
     */
40
    public function __construct(TransportInterface $serviceTransport)
41
    {
42
        try {
43
            $this->serviceTransport = $serviceTransport;
44
45
            $this->initCustomRoutes();
46
47
            $this->fetchActions();
48
        } catch (\Exception $e) {
49
            $this->getTransport()->handleException($e);
50
        }
51
    }
52
53
    /**
54
     * Method fetches actions if they are existing
55
     */
56
    protected function fetchActions(): void
57
    {
58
        if ($this instanceof ServiceBaseLogicInterface) {
59
            $this->serviceTransport->fetchActions($this);
60
        }
61
62
        // TODO move to the Transport class
63
        foreach ($this->serviceTransport->getServiceLogics() as $actionsSet) {
64
            if ($actionsSet instanceof ServiceBaseLogicInterface) {
65
                $this->serviceTransport->fetchActions($actionsSet);
66
            }
67
        }
68
    }
69
70
    /**
71
     * Method constructs service logic if necessary
72
     *
73
     * @param mixed $serviceLogic
74
     *            Service logic class name of object itself
75
     * @param mixed $serviceModel
76
     *            Service model class name of object itself
77
     * @return ServiceLogic logic object
78
     */
79
    protected function constructServiceLogic($serviceLogic, $serviceModel)
80
    {
81
        if (is_string($serviceLogic)) {
82
            $result = new $serviceLogic(
83
                $this->serviceTransport->getParamsFetcher(),
84
                $this->serviceTransport->getSecurityProvider(),
85
                $serviceModel);
86
        } else {
87
            $result = $serviceLogic;
88
        }
89
90
        return $result;
91
    }
92
93
    /**
94
     * Method inits service's logic
95
     *
96
     * @param mixed $serviceLogic
97
     *            Service's logic
98
     * @param mixed $serviceModel
99
     *            Service's Model
100
     */
101
    protected function initServiceLogic($serviceLogic, $serviceModel): void
102
    {
103
        if (is_array($serviceLogic)) {
104
            $this->serviceLogics = [];
0 ignored issues
show
Bug Best Practice introduced by
The property serviceLogics does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
105
106
            foreach ($serviceLogic as $logic) {
107
                $this->serviceLogics[] = $this->constructServiceLogic($logic, $serviceModel);
108
            }
109
        } else {
110
            $this->serviceLogics = [
111
                $this->constructServiceLogic($serviceLogic, $serviceModel)
112
            ];
113
        }
114
115
        $this->serviceTransport->setServiceLogics($this->serviceLogics);
116
    }
117
118
    /**
119
     * Method inits custom routes if necessary
120
     */
121
    protected function initCustomRoutes(): void
122
    {
123
        $reflector = new \ReflectionClass(get_class($this));
124
        $classPath = dirname($reflector->getFileName());
125
126
        // TODO make /Conf/...
127
        if (file_exists($classPath . '/conf/routes.php')) {
128
            $this->serviceTransport->loadRoutesFromConfig($classPath . '/conf/routes.php');
129
        }
130
131
        if (file_exists($classPath . '/conf/routes.json')) {
132
            $this->serviceTransport->loadRoutes(json_decode(file_get_contents($classPath . '/conf/routes.json'), true));
133
        }
134
    }
135
136
    /**
137
     * Running $this->serviceTransport run loop
138
     */
139
    public function run(): void
140
    {
141
        $this->serviceTransport->run();
142
    }
143
144
    /**
145
     * Method sets transport
146
     *
147
     * @param Transport $transport
148
     */
149
    public function setTransport(Transport $transport): void
150
    {
151
        $this->serviceTransport = $transport;
152
    }
153
154
    /**
155
     * Method returns transport
156
     *
157
     * @return Transport
158
     */
159
    public function getTransport(): Transport
160
    {
161
        return $this->serviceTransport;
162
    }
163
164
    /**
165
     * Method returns logic objects
166
     *
167
     * @return ServiceLogic[]
168
     */
169
    public function getLogics()
170
    {
171
        return $this->serviceLogics;
172
    }
173
}
174