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

FileStorageDataLayerContainer   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
b 0
f 0
dl 0
loc 40
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getCurrentFileStorageDataLayer() 0 17 3
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