Passed
Push — master ( d0ff49...4cf824 )
by Alex
03:05
created

ServiceBase::getLogics()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
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;
30
31
    /**
32
     * Constructor
33
     *
34
     * @param TransportInterface $serviceTransport
35
     *            service's transport
36
     */
37
    public function __construct(TransportInterface $serviceTransport)
38
    {
39
        try {
40
            $this->serviceTransport = $serviceTransport;
41
42
            $this->initCustomRoutes();
43
44
            $this->fetchActions();
45
        } catch (\Exception $e) {
46
            $this->getTransport()->handleException($e);
47
        }
48
    }
49
50
    /**
51
     * Method fetches actions if they are existing
52
     */
53
    protected function fetchActions(): void
54
    {
55
        if ($this instanceof ServiceBaseLogicInterface) {
56
            $this->serviceTransport->fetchActions($this);
57
        }
58
59
        // TODO move to the Transport class
60
        foreach ($this->serviceTransport->getServiceLogics() as $actionsSet) {
61
            if ($actionsSet instanceof ServiceBaseLogicInterface) {
62
                $this->serviceTransport->fetchActions($actionsSet);
63
            }
64
        }
65
    }
66
67
    /**
68
     * Method inits custom routes if necessary
69
     */
70
    protected function initCustomRoutes(): void
71
    {
72
        $reflector = new \ReflectionClass(get_class($this));
73
        $classPath = dirname($reflector->getFileName());
74
75
        // TODO make /Conf/...
76
        if (file_exists($classPath . '/conf/routes.php')) {
77
            $this->serviceTransport->loadRoutesFromConfig($classPath . '/conf/routes.php');
78
        }
79
80
        if (file_exists($classPath . '/conf/routes.json')) {
81
            $this->serviceTransport->loadRoutes(json_decode(file_get_contents($classPath . '/conf/routes.json'), true));
82
        }
83
    }
84
85
    /**
86
     * Running $this->serviceTransport run loop
87
     */
88
    public function run(): void
89
    {
90
        $this->serviceTransport->run();
91
    }
92
93
    /**
94
     * Method sets transport
95
     *
96
     * @param Transport $transport
97
     */
98
    public function setTransport(Transport $transport): void
99
    {
100
        $this->serviceTransport = $transport;
101
    }
102
103
    /**
104
     * Method returns transport
105
     *
106
     * @return Transport
107
     */
108
    public function getTransport(): Transport
109
    {
110
        return $this->serviceTransport;
111
    }
112
}
113