LazyFilesystemObjectStorage   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Test Coverage

Coverage 80.95%

Importance

Changes 0
Metric Value
dl 0
loc 59
ccs 17
cts 21
cp 0.8095
rs 10
c 0
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A storeObject() 0 5 1
A getObject() 0 20 3
1
<?php
2
3
/*
4
 * This file is part of the Runalyze BSON.
5
 * (c) RUNALYZE <[email protected]>
6
 * This source file is subject to the MIT license that is bundled
7
 * with this source code in the file LICENSE.
8
 */
9
10
namespace Runalyze\BSON\LazyFilesystemObject;
11
12
use League\Flysystem\Filesystem;
13
use ProxyManager\Configuration;
14
use ProxyManager\Factory\LazyLoadingValueHolderFactory;
15
use Runalyze\BSON\BsonSerializable\BsonSerializableInterface;
16
17
class LazyFilesystemObjectStorage
18
{
19
    /** @var Filesystem */
20
    protected $Filesystem;
21
22
    /** @var HashToPathMapperInterface */
23
    protected $PathMapper;
24
25
    /** @var LazyLoadingValueHolderFactory */
26
    protected $ProxyFactory;
27
28 1
    public function __construct(
29
        Filesystem $filesystem,
30
        HashToPathMapperInterface $pathMapper,
31
        Configuration $proxyConfiguration = null
32
    ) {
33 1
        $this->Filesystem = $filesystem;
34 1
        $this->PathMapper = $pathMapper;
35 1
        $this->ProxyFactory = new LazyLoadingValueHolderFactory($proxyConfiguration);
36 1
    }
37
38
    /**
39
     * @param  string                    $hash
40
     * @param  string                    $class
41
     * @return BsonSerializableInterface
42
     */
43 1
    public function getObject($hash, $class)
44
    {
45 1
        $path = $this->PathMapper->hashToPath($hash);
46
47 1
        return $this->ProxyFactory->createProxy(
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->ProxyFacto...ion(...) { /* ... */ }) returns the type ProxyManager\Proxy\VirtualProxyInterface which is incompatible with the documented return type Runalyze\BSON\BsonSerial...onSerializableInterface.
Loading history...
48 1
            $class,
49 1
            function (&$wrappedObject, $proxy, $method, $parameters, &$initializer) use ($path, $class) {
50
                try {
51 1
                    $contents = $this->Filesystem->read($path);
52
                } catch (\Exception $e) {
53
                    throw $e; // TODO
54
                }
55
56
                try {
57 1
                    $wrappedObject = \MongoDB\BSON\toPHP($contents, ['root' => $class]);
0 ignored issues
show
Bug introduced by
It seems like $contents can also be of type false; however, parameter $bson of MongoDB\BSON\toPHP() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

57
                    $wrappedObject = \MongoDB\BSON\toPHP(/** @scrutinizer ignore-type */ $contents, ['root' => $class]);
Loading history...
58
                } catch (\Exception $e) {
59
                    throw $e; // TODO
60
                }
61
62 1
                $initializer = null;
63 1
            }
64
        );
65
    }
66
67
    /**
68
     * @param string                    $hash
69
     * @param BsonSerializableInterface $object
70
     */
71 1
    public function storeObject($hash, BsonSerializableInterface $object)
72
    {
73 1
        $path = $this->PathMapper->hashToPath($hash);
74
75 1
        $this->Filesystem->put($path, $object->toBinary());
76 1
    }
77
}
78