ConditionalPathBuilder::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
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<int, array{callable: callable, pathBuilder: \Phauthentic\Infrastructure\Storage\PathBuilder\PathBuilderInterface}>
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
    public function __construct(PathBuilderInterface $pathBuilder)
45
    {
46
        $this->defaultPathBuilder = $pathBuilder;
47
    }
48
49
    /**
50
     * @param \Phauthentic\Infrastructure\Storage\PathBuilder\PathBuilderInterface $pathBuilder Path Builder
51
     * @param callable $conditionCheck Condition check
52
     * @return $this
53
     */
54
    public function addPathBuilder(PathBuilderInterface $pathBuilder, callable $conditionCheck): self
55
    {
56
        $this->pathBuilders[] = [
57
            'callable' => $conditionCheck,
58
            'pathBuilder' => $pathBuilder
59
        ];
60
61
        return $this;
62
    }
63
64
    /**
65
     * @inheritDoc
66
     * @param array<string, mixed> $options
67
     */
68
    public function path(FileInterface $file, array $options = []): string
69
    {
70
        foreach ($this->pathBuilders as $builder) {
71
            if ($builder['callable']($file)) {
72
                return $builder['pathBuilder']->path($file, $options);
73
            }
74
        }
75
76
        return $this->defaultPathBuilder->path($file, $options);
77
    }
78
79
    /**
80
     * @inheritDoc
81
     * @param array<string, mixed> $options
82
     */
83
    public function pathForVariant(FileInterface $file, string $name, array $options = []): string
84
    {
85
        foreach ($this->pathBuilders as $builder) {
86
            if ($builder['callable']($file)) {
87
                return $builder['pathBuilder']->pathForVariant($file, $name, $options);
88
            }
89
        }
90
91
        return $this->defaultPathBuilder->pathForVariant($file, $name, $options);
92
    }
93
}
94