Completed
Push — master ( c598c9...807440 )
by Alex
02:35
created

ServiceBase.php (1 issue)

Severity
1
<?php
2
namespace Mezon\Service;
3
4
/**
5
 * Class Service
6
 *
7
 * @package Mezon
8
 * @subpackage ServiceBase
9
 * @author Dodonov A.A.
10
 * @version v.1.0 (2019/12/09)
11
 * @copyright Copyright (c) 2019, aeon.org
12
 */
13
14
/**
15
 * Base service class
16
 *
17
 * It bounds together transport, request parameters fetcher, logic, authorization and model
18
 *
19
 * @author Dodonov A.A.
20
 */
21
class ServiceBase
22
{
23
24
    /**
25
     * Service's ransport
26
     *
27
     * @var object Service transport object
28
     */
29
    private $serviceTransport = false;
30
31
    /**
32
     * Service's logic
33
     *
34
     * @var \Mezon\Service\ServiceLogic|array Login object or list of logic objects
35
     */
36
    private $serviceLogic = false;
37
38
    /**
39
     * Constructor
40
     *
41
     * @param mixed $serviceLogic
42
     *            Service's logic
43
     * @param mixed $serviceModel
44
     *            Service's model
45
     * @param mixed $securityProvider
46
     *            Service's security provider
47
     * @param mixed $serviceTransport
48
     *            Service's transport
49
     */
50
    public function __construct(
51
        $serviceLogic = \Mezon\Service\ServiceBaseLogic::class,
52
        $serviceModel = \Mezon\Service\ServiceModel::class,
53
        $securityProvider = \Mezon\Service\ServiceMockSecurityProvider::class,
54
        $serviceTransport = \Mezon\Service\ServiceRestTransport\ServiceRestTransport::class)
55
    {
56
        try {
57
            $this->initTransport($serviceTransport, $securityProvider);
58
59
            $this->initServiceLogic($serviceLogic, $serviceModel);
60
61
            $this->initCustomRoutes();
62
63
            $this->fetchActions();
64
        } catch (\Exception $e) {
65
            $this->getTransport()->handleException($e);
66
        }
67
    }
68
69
    /**
70
     * Method fetches actions if they are existing
71
     */
72
    protected function fetchActions(): void
73
    {
74
        if ($this instanceof \Mezon\Service\ServiceBaseLogicInterface) {
75
            $this->serviceTransport->fetchActions($this);
76
        }
77
78
        if ($this->serviceLogic instanceof \Mezon\Service\ServiceBaseLogicInterface) {
79
            $this->serviceTransport->fetchActions($this->serviceLogic);
80
        } elseif (is_array($this->serviceLogic)) {
0 ignored issues
show
The condition is_array($this->serviceLogic) is always true.
Loading history...
81
            foreach ($this->serviceLogic as $actionsSet) {
82
                if ($actionsSet instanceof \Mezon\Service\ServiceBaseLogicInterface) {
83
                    $this->serviceTransport->fetchActions($actionsSet);
84
                }
85
            }
86
        }
87
    }
88
89
    /**
90
     * Method inits service's transport
91
     *
92
     * @param mixed $serviceTransport
93
     *            Service's transport
94
     * @param mixed $securityProvider
95
     *            Service's security provider
96
     */
97
    protected function initTransport($serviceTransport, $securityProvider): void
98
    {
99
        if (is_string($serviceTransport)) {
100
            $this->serviceTransport = new $serviceTransport($securityProvider);
101
        } else {
102
            $this->serviceTransport = $serviceTransport;
103
        }
104
    }
105
106
    /**
107
     * Method constructs service logic if necessary
108
     *
109
     * @param mixed $serviceLogic
110
     *            Service logic class name of object itself
111
     * @param mixed $serviceModel
112
     *            Service model class name of object itself
113
     * @return \Mezon\Service\ServiceLogic logic object
114
     */
115
    protected function constructServiceLogic($serviceLogic, $serviceModel)
116
    {
117
        if (is_string($serviceLogic)) {
118
            $result = new $serviceLogic(
119
                $this->serviceTransport->getParamsFetcher(),
120
                $this->serviceTransport->securityProvider,
121
                $serviceModel);
122
        } else {
123
            $result = $serviceLogic;
124
        }
125
126
        return $result;
127
    }
128
129
    /**
130
     * Method inits service's logic
131
     *
132
     * @param mixed $serviceLogic
133
     *            Service's logic
134
     * @param mixed $serviceModel
135
     *            Service's Model
136
     */
137
    protected function initServiceLogic($serviceLogic, $serviceModel): void
138
    {
139
        if (is_array($serviceLogic)) {
140
            $this->serviceLogic = [];
141
142
            foreach ($serviceLogic as $logic) {
143
                $this->serviceLogic[] = $this->constructServiceLogic($logic, $serviceModel);
144
            }
145
        } else {
146
            $this->serviceLogic = $this->constructServiceLogic($serviceLogic, $serviceModel);
147
        }
148
149
        $this->serviceTransport->setServiceLogic($this->serviceLogic);
150
    }
151
152
    /**
153
     * Method inits custom routes if necessary
154
     */
155
    protected function initCustomRoutes(): void
156
    {
157
        $reflector = new \ReflectionClass(get_class($this));
158
        $classPath = dirname($reflector->getFileName());
159
160
        if (file_exists($classPath . '/conf/routes.php')) {
161
            $this->serviceTransport->loadRoutesFromConfig($classPath . '/conf/routes.php');
162
        }
163
164
        if (file_exists($classPath . '/conf/routes.json')) {
165
            $this->serviceTransport->loadRoutes(json_decode(file_get_contents($classPath . '/conf/routes.json'), true));
166
        }
167
    }
168
169
    /**
170
     * Running $this->serviceTransport run loop
171
     */
172
    public function run(): void
173
    {
174
        $this->serviceTransport->run();
175
    }
176
177
    /**
178
     * Method sets transport
179
     *
180
     * @param \Mezon\Service\ServiceTransport $transport
181
     */
182
    public function setTransport(\Mezon\Service\ServiceTransport $transport): void
183
    {
184
        $this->serviceTransport = $transport;
185
    }
186
187
    /**
188
     * Method returns transport
189
     *
190
     * @return \Mezon\Service\ServiceTransport
191
     */
192
    public function getTransport(): \Mezon\Service\ServiceTransport
193
    {
194
        return $this->serviceTransport;
195
    }
196
197
    /**
198
     * Method returns logic
199
     *
200
     * @return \Mezon\Service\ServiceLogic|array
201
     */
202
    public function getLogic()
203
    {
204
        return $this->serviceLogic;
205
    }
206
}
207