Passed
Push — master ( c12552...22ad80 )
by Gabor
04:56
created

FilesystemProxy::resolveMiddleware()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 28
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 28
ccs 0
cts 20
cp 0
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 15
nc 4
nop 2
crap 20
1
<?php
2
/**
3
 * WebHemi.
4
 *
5
 * PHP version 7.1
6
 *
7
 * @copyright 2012 - 2017 Gixx-web (http://www.gixx-web.com)
8
 * @license   https://opensource.org/licenses/MIT The MIT License (MIT)
9
 *
10
 * @link      http://www.gixx-web.com
11
 */
12
declare(strict_types = 1);
13
14
namespace WebHemi\Router\Proxy;
15
16
use WebHemi\Data\Storage;
17
use WebHemi\Data\Entity;
18
use WebHemi\Data\StorageInterface;
19
use WebHemi\Router\ProxyInterface;
20
use WebHemi\Router\Result\Result;
21
22
/**
23
 * Class FilesystemProxy
24
 */
25
class FilesystemProxy implements ProxyInterface
26
{
27
    /** @var StorageInterface[] */
28
    private $dataStorages = [];
29
30
    /**
31
     * FilesystemProxy constructor.
32
     *
33
     * @param StorageInterface[] ...$dataStorages
34
     */
35
    public function __construct(StorageInterface ...$dataStorages)
36
    {
37
        foreach ($dataStorages as $instance) {
38
            $storageClass = get_class($instance);
39
40
            $this->dataStorages[$storageClass] = $instance;
41
        }
42
    }
43
44
    /**
45
     * Resolves the middleware class name for the application and URL.
46
     *
47
     * @param string $application
48
     * @param Result $routeResult
49
     * @return void
50
     */
51
    public function resolveMiddleware(string $application, Result &$routeResult) : void
52
    {
53
        $applicationEntity = $this->getApplicationEntity($application);
54
55
        if (!$applicationEntity) {
56
            return;
57
        }
58
59
        $parameters = $routeResult->getParameters();
60
        $fileSystemEntity = $this->getFilesystemEntityByRouteParams($applicationEntity, $parameters);
61
62
        if (!$fileSystemEntity) {
63
            return;
64
        }
65
66
        if ($fileSystemEntity->getType() == Entity\Filesystem\FilesystemEntity::TYPE_DIRECTORY) {
67
            // DirectoryId must exists, as well as the relevant directory entity...
68
            $fileSystemDirectoryEntity = $this->getFilesystemDirectoryEntity($fileSystemEntity->getDirectoryId());
69
            // Theoretically this alway should be valid, since the proxy is not editable
70
            $middleware = $fileSystemDirectoryEntity->getProxy() ?? self::LIST_POST;
71
72
            $routeResult->setMatchedMiddleware($middleware);
73
        } else {
74
            $routeResult->setMatchedMiddleware(self::VIEW_POST);
75
        }
76
77
        $routeResult->setParameters($parameters);
78
    }
79
80
    /**
81
     * Gets the application entity.
82
     *
83
     * @param string $application
84
     * @return Entity\ApplicationEntity
85
     */
86
    private function getApplicationEntity(string $application) : ? Entity\ApplicationEntity
87
    {
88
        /** @var Storage\ApplicationStorage $applicationStorage */
89
        $applicationStorage = $this->dataStorages[Storage\ApplicationStorage::class] ?? null;
90
91
        if (!$applicationStorage) {
92
            return null;
93
        }
94
95
        return $applicationStorage->getApplicationByName($application);
96
    }
97
98
    /**
99
     * Gets the filesystem entity.
100
     *
101
     * @param Entity\ApplicationEntity $applicationEntity
102
     * @param array $parameters
103
     * @return null|Entity\Filesystem\FilesystemEntity
104
     */
105
    private function getFilesystemEntityByRouteParams(
106
        Entity\ApplicationEntity $applicationEntity,
107
        array &$parameters
108
    ) : ? Entity\Filesystem\FilesystemEntity {
109
        /** @var Storage\Filesystem\FilesystemStorage $fileSystemStorage */
110
        $fileSystemStorage = $this->dataStorages[Storage\Filesystem\FilesystemStorage::class] ?? null;
111
112
        if (!$fileSystemStorage) {
113
            return null;
114
        }
115
116
        $path = $parameters['path'];
117
        $baseName = $parameters['basename'];
118
119
        $fileSystemEntity = $fileSystemStorage->getFilesystemData(
120
            $applicationEntity->getApplicationId(),
121
            $path,
122
            $baseName
123
        );
124
125
        // If we don't find it as a created content, we try with the preserved contents
126
        if (!$fileSystemEntity && $path != '/') {
127
            $uri = trim($path.'/'.$baseName, '/');
128
            $parts = explode('/', $uri);
129
130
            $parameters = [
131
                'path' => '/',
132
                'basename' => array_shift($parts),
133
                'uri_parameter' => implode('/', $parts)
134
            ];
135
136
            $fileSystemEntity = $this->getFilesystemEntityByRouteParams($applicationEntity, $parameters);
137
        }
138
139
        return $fileSystemEntity;
140
    }
141
142
    /**
143
     * Gets the directory entity.
144
     *
145
     * @param int $identifier
146
     * @return null|Entity\Filesystem\FilesystemDirectoryEntity
147
     */
148
    private function getFilesystemDirectoryEntity(int $identifier) : ? Entity\Filesystem\FilesystemDirectoryEntity
149
    {
150
        /** @var Storage\Filesystem\FilesystemDirectoryStorage $fileSystemDirectoryStorage */
151
        $fileSystemDirectoryStorage = $this->dataStorages[Storage\Filesystem\FilesystemDirectoryStorage::class] ?? null;
152
153
        if (!$fileSystemDirectoryStorage) {
154
            return null;
155
        }
156
157
        return $fileSystemDirectoryStorage->getFilesystemDirectoryById($identifier);
158
    }
159
}
160