Passed
Push — master ( c5bd37...640ffc )
by
unknown
05:22
created

FileStorageDataLayerContainer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 4
rs 10
1
<?php
2
3
namespace W2w\Laravel\Apie\Services;
4
5
use W2w\Lib\Apie\Plugins\FileStorage\DataLayers\FileStorageDataLayer;
6
7
class FileStorageDataLayerContainer
8
{
9
    /**
10
     * @var ApieContext
11
     */
12
    private $apieContext;
13
14
    /**
15
     * @var string
16
     */
17
    private $storagePath;
18
19
    /**
20
     * @var FileStorageDataLayer[]
21
     */
22
    private $instantiated = [];
23
24
    public function __construct(string $storagePath, ApieContext $apieContext)
25
    {
26
        $this->storagePath = $storagePath;
27
        $this->apieContext = $apieContext;
28
    }
29
30
    public function getCurrentFileStorageDataLayer(): FileStorageDataLayer
31
    {
32
        $apieContext = $this->apieContext->getActiveContext();
33
        $apie = $apieContext->getApie();
34
35
        $context = implode('.', $apieContext->getContextKey());
36
        // make sure we keep the root Apie instance in a separate folder.
37
        if (!$context) {
38
            $context = '-';
39
        }
40
        if (!isset($this->instantiated[$context])) {
41
            $this->instantiated[$context] = new FileStorageDataLayer(
42
                $this->storagePath . DIRECTORY_SEPARATOR . $context,
43
                $apie->getPropertyAccessor()
44
            );
45
        }
46
        return $this->instantiated[$context];
47
    }
48
}
49