Issues (150)

src/Blocks/Routes.php (2 issues)

Severity
1
<?php
2
3
namespace SoliDry\Blocks;
4
5
use SoliDry\Extension\JSONApiInterface;
6
use SoliDry\Helpers\Classes;
7
use SoliDry\Helpers\Console;
8
use SoliDry\ApiGenerator;
9
use SoliDry\Types\DefaultInterface;
10
use SoliDry\Types\PhpInterface;
11
use SoliDry\Types\RoutesInterface;
12
13
class Routes
14
{
15
16
    use ContentManager, RoutesTrait;
0 ignored issues
show
The trait SoliDry\Blocks\RoutesTrait requires some properties which are not provided by SoliDry\Blocks\Routes: $version, $objectName
Loading history...
The trait SoliDry\Blocks\ContentManager requires some properties which are not provided by SoliDry\Blocks\Routes: $options, $version, $modulesDir
Loading history...
17
18
    /**
19
     * @var ApiGenerator
20
     */
21
    private ApiGenerator $generator;
22
23
    /**
24
     * @var string
25
     */
26
    protected string $sourceCode = '';
27
28
    /**
29
     * @var string
30
     */
31
    private string $className;
32
33
    /**
34
     * Routes constructor.
35
     * @param $generator
36
     */
37
    public function __construct($generator)
38
    {
39
        $this->generator = $generator;
40
        $this->className = Classes::getClassName($this->generator->objectName);
41
    }
42
43
    /**
44
     * @param $generator
45
     */
46
    public function setCodeState($generator): void
47
    {
48
        $this->generator = $generator;
49
    }
50
51
    public function create(): void
52
    {
53
        $this->setRoutes();
54
        $isCreated = false;
55
56
        $file = FileManager::getModulePath($this->generator) .
57
            RoutesInterface::ROUTES_DIR . DIRECTORY_SEPARATOR . RoutesInterface::ROUTES_FILE_NAME . PhpInterface::PHP_EXT;
58
59
        // TODO: fix this behaviour - collect data 1-st for ex.
60
        if ($this->generator->routesCreated === 0 || file_exists($file) === false) {
61
            $isCreated = FileManager::createFile($file, $this->sourceCode, true);
62
        } else {
63
            $this->sourceCode = str_replace(PhpInterface::PHP_OPEN_TAG, '', $this->sourceCode);
64
            file_put_contents($file, $this->sourceCode, FILE_APPEND);
65
        }
66
67
        ++$this->generator->routesCreated;
68
        if ($isCreated) {
69
            Console::out($file . PhpInterface::SPACE . Console::CREATED, Console::COLOR_GREEN);
70
        }
71
    }
72
73
    private function setRoutes(): void
74
    {
75
        $this->setTag();
76
        $this->setComment(DefaultInterface::ROUTES_START, 0);
77
        $this->setComment($this->className . ' routes', 0);
78
        $this->openGroup();
79
80
        // create bulk api-calls
81
        $this->setBulkRoutes();
82
83
        // create basic api-calls of JSON API
84
        $this->setBasicRoutes();
85
86
        // create relations process routes
87
        $this->setRelationsRoutes();
88
        $this->closeGroup();
89
        $this->setComment(DefaultInterface::ROUTES_END, 0);
90
    }
91
92
    private function setBulkRoutes(): void
93
    {
94
        $this->setComment('bulk routes');
95
96
        $this->setRoute(RoutesInterface::METHOD_POST, $this->composeBulkUri(),
97
            $this->composeEndPoint(JSONApiInterface::URI_METHOD_CREATE_BULK));
98
        $this->setRoute(RoutesInterface::METHOD_PATCH, $this->composeBulkUri(),
99
            $this->composeEndPoint(JSONApiInterface::URI_METHOD_UPDATE_BULK));
100
        $this->setRoute(RoutesInterface::METHOD_DELETE, $this->composeBulkUri(),
101
            $this->composeEndPoint(JSONApiInterface::URI_METHOD_DELETE_BULK));
102
    }
103
104
    private function setBasicRoutes(): void
105
    {
106
        $this->setComment('basic routes');
107
108
        $this->setRoute(RoutesInterface::METHOD_OPTIONS, $this->composeObjectUri(),
109
            $this->composeEndPoint(JSONApiInterface::URI_METHOD_OPTIONS));
110
        $this->setRoute(RoutesInterface::METHOD_GET, $this->composeObjectUri(),
111
            $this->composeEndPoint(JSONApiInterface::URI_METHOD_INDEX));
112
        $this->setRoute(RoutesInterface::METHOD_GET, $this->composeIdUri(),
113
            $this->composeEndPoint(JSONApiInterface::URI_METHOD_VIEW));
114
        $this->setRoute(RoutesInterface::METHOD_POST, $this->composeObjectUri(),
115
            $this->composeEndPoint(JSONApiInterface::URI_METHOD_CREATE));
116
        $this->setRoute(RoutesInterface::METHOD_PATCH, $this->composeIdUri(),
117
            $this->composeEndPoint(JSONApiInterface::URI_METHOD_UPDATE));
118
        $this->setRoute(RoutesInterface::METHOD_DELETE, $this->composeIdUri(),
119
            $this->composeEndPoint(JSONApiInterface::URI_METHOD_DELETE));
120
    }
121
122
    private function setRelationsRoutes(): void
123
    {
124
        $this->setComment('relation routes');
125
126
        // getters
127
        $this->setRoute(RoutesInterface::METHOD_GET, $this->composeRelatedUri(),
128
            $this->composeEndPoint(JSONApiInterface::URI_METHOD_RELATED));
129
        $this->setRoute(RoutesInterface::METHOD_GET, $this->composeRelationsUri(),
130
            $this->composeEndPoint(JSONApiInterface::URI_METHOD_RELATIONS));
131
132
        // mutators
133
        $this->setRoute(RoutesInterface::METHOD_POST, $this->composeRelationsUri(),
134
            $this->composeEndPoint(JSONApiInterface::URI_METHOD_CREATE
135
                . ucfirst(JSONApiInterface::URI_METHOD_RELATIONS)));
136
        $this->setRoute(RoutesInterface::METHOD_PATCH, $this->composeRelationsUri(),
137
            $this->composeEndPoint(JSONApiInterface::URI_METHOD_UPDATE
138
                . ucfirst(JSONApiInterface::URI_METHOD_RELATIONS)));
139
        $this->setRoute(RoutesInterface::METHOD_DELETE, $this->composeRelationsUri(),
140
            $this->composeEndPoint(JSONApiInterface::URI_METHOD_DELETE
141
                . ucfirst(JSONApiInterface::URI_METHOD_RELATIONS)));
142
    }
143
}