Passed
Push — master ( a4ef1c...1fb27f )
by Florian
12:17 queued 12s
created

ConditionalPathBuilder   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Test Coverage

Coverage 94.44%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
b 0
f 0
dl 0
loc 61
ccs 17
cts 18
cp 0.9444
rs 10
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A addPathBuilder() 0 8 1
A path() 0 9 3
A pathForVariant() 0 9 3
1
<?php
2
3
/**
4
 * Copyright (c) Florian Krämer (https://florian-kraemer.net)
5
 * Licensed under The MIT License
6
 * For full copyright and license information, please see the LICENSE.txt
7
 * Redistributions of files must retain the above copyright notice.
8
 *
9
 * @copyright Copyright (c) Florian Krämer (https://florian-kraemer.net)
10
 * @author    Florian Krämer
11
 * @link      https://github.com/Phauthentic
12
 * @license   https://opensource.org/licenses/MIT MIT License
13
 */
14
15
declare(strict_types=1);
16
17
namespace Phauthentic\Infrastructure\Storage\PathBuilder;
18
19
use Phauthentic\Infrastructure\Storage\FileInterface;
20
21
/**
22
 * Add callbacks and path builders to check on the file which of the builders
23
 * should be used to build the path.
24
 *
25
 * This allows you to use different instances with a different configuration or
26
 * implementation of path builders for files of different types or in different
27
 * collections or models.
28
 */
29
class ConditionalPathBuilder implements PathBuilderInterface
30
{
31
    /**
32
     * @var array
33
     */
34
    protected array $pathBuilders = [];
35
36
    /**
37
     * @var \Phauthentic\Infrastructure\Storage\PathBuilder\PathBuilderInterface
38
     */
39
    protected PathBuilderInterface $defaultPathBuilder;
40
41
    /**
42
     * @param \Phauthentic\Infrastructure\Storage\PathBuilder\PathBuilderInterface $pathBuilder Default Path Builder
43
     */
44 2
    public function __construct(PathBuilderInterface $pathBuilder)
45
    {
46 2
        $this->defaultPathBuilder = $pathBuilder;
47 2
    }
48
49
    /**
50
     * @param \Phauthentic\Infrastructure\Storage\PathBuilder\PathBuilderInterface $pathBuilder Path Builder
51
     * @param callable $conditionCheck Condition check
52
     * @return $this
53
     */
54 2
    public function addPathBuilder(PathBuilderInterface $pathBuilder, callable $conditionCheck): self
55
    {
56 2
        $this->pathBuilders[] = [
57 2
            'callable' => $conditionCheck,
58 2
            'pathBuilder' => $pathBuilder
59
        ];
60
61 2
        return $this;
62
    }
63
64
    /**
65
     * @inheritDoc
66
     */
67 1
    public function path(FileInterface $file, array $options = []): string
68
    {
69 1
        foreach ($this->pathBuilders as $builder) {
70 1
            if ($builder['callable']($file)) {
71 1
                return $builder['pathBuilder']->path($file, $options);
72
            }
73
        }
74
75 1
        return $this->defaultPathBuilder->path($file, $options);
76
    }
77
78
    /**
79
     * @inheritDoc
80
     */
81 1
    public function pathForVariant(FileInterface $file, string $name, array $options = []): string
82
    {
83 1
        foreach ($this->pathBuilders as $builder) {
84 1
            if ($builder['callable']($file)) {
85
                return $builder['pathBuilder']->pathForVariant($file, $name, $options);
86
            }
87
        }
88
89 1
        return $this->defaultPathBuilder->pathForVariant($file, $name, $options);
90
    }
91
}
92