Completed
Push — master ( afaf42...8ac772 )
by Raffael
20:10 queued 16:21
created

Factory::build()   B

Complexity

Conditions 7
Paths 11

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
dl 0
loc 26
ccs 0
cts 16
cp 0
rs 8.5706
c 0
b 0
f 0
cc 7
nc 11
nop 3
crap 56
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * balloon
7
 *
8
 * @copyright   Copryright (c) 2012-2018 gyselroth GmbH (https://gyselroth.com)
9
 * @license     GPL-3.0 https://opensource.org/licenses/GPL-3.0
10
 */
11
12
namespace Balloon\Filesystem\Node;
13
14
use Balloon\Filesystem;
15
use Balloon\Filesystem\Acl;
16
use Balloon\Filesystem\Exception;
17
use Balloon\Filesystem\Storage;
18
use Balloon\Filesystem\Storage\Adapter\AdapterInterface as StorageAdapterInterface;
19
use Balloon\Filesystem\Storage\Factory as StorageFactory;
20
use Balloon\Hook;
21
use Balloon\Server;
22
use MongoDB\BSON\ObjectId;
23
use MongoDB\Database;
24
use Psr\Log\LoggerInterface;
25
26
class Factory
27
{
28
    /**
29
     * Database.
30
     *
31
     * @var Database
32
     */
33
    protected $db;
34
35
    /**
36
     * LoggerInterface.
37
     *
38
     * @var LoggerInterface
39
     */
40
    protected $logger;
41
42
    /**
43
     * Hook.
44
     *
45
     * @var Hook
46
     */
47
    protected $hook;
48
49
    /**
50
     * Server.
51
     *
52
     * @var Server
53
     */
54
    protected $server;
55
56
    /**
57
     * Storage.
58
     *
59
     * @var StorageAdapterInterface
60
     */
61
    protected $storage;
62
63
    /**
64
     * Storage Factory.
65
     *
66
     * @var StorageFactory
67
     */
68
    protected $storage_factory;
69
70
    /**
71
     * Acl.
72
     *
73
     * @var Acl
74
     */
75
    protected $acl;
76
77
    /**
78
     * Storage cache.
79
     *
80
     * @var array
81
     */
82
    protected $cache = [];
83
84
    /**
85
     * Initialize.
86
     */
87
    public function __construct(Database $db, Hook $hook, LoggerInterface $logger, StorageAdapterInterface $storage, Acl $acl, StorageFactory $storage_factory)
88
    {
89
        $this->db = $db;
90
        $this->logger = $logger;
91
        $this->hook = $hook;
92
        $this->storage = $storage;
93
        $this->acl = $acl;
94
        $this->storage_factory = $storage_factory;
95
    }
96
97
    /**
98
     * Build node instance.
99
     */
100
    public function build(Filesystem $fs, array $node, ?Collection $parent): NodeInterface
101
    {
102
        if (!isset($node['directory'])) {
103
            throw new Exception('invalid node ['.$node['_id'].'] found, directory attribute does not exists');
104
        }
105
106
        $storage = $this->storage;
107
108
        if (isset($node['reference'])) {
109
            $share = $fs->findRawNode($node['reference']);
110
            if (isset($share['mount'])) {
111
                $storage = $this->getStorage($share['_id'], $share['mount']);
112
            }
113
        } elseif (isset($node['storage_reference'])) {
114
            $external = $fs->findNodeById($node['storage_reference'])->getAttributes()['mount'];
115
            $storage = $this->getStorage($node['storage_reference'], $external);
116
        } elseif (isset($node['mount'])) {
117
            $storage = $this->getStorage($node['_id'], $node['mount']);
118
        }
119
120
        if (true === $node['directory']) {
121
            return new Collection($node, $fs, $this->logger, $this->hook, $this->acl, $parent, $storage);
122
        }
123
124
        return new File($node, $fs, $this->logger, $this->hook, $this->acl, $parent);
125
    }
126
127
    /**
128
     * Get by id.
129
     */
130
    protected function getStorage(ObjectId $node, array $mount): StorageAdapterInterface
131
    {
132
        $id = (string) $node;
133
134
        if (isset($this->cache[$id])) {
135
            return $this->cache[$id];
136
        }
137
138
        return $this->cache[$id] = $this->storage_factory->build($mount);
139
    }
140
}
141