Passed
Pull Request — master (#406)
by Théo
02:17
created

Compactors   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 12
eloc 19
dl 0
loc 53
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A toArray() 0 3 1
A getScoper() 0 3 2
A getScoperWhitelist() 0 3 2
A compactContents() 0 8 1
A registerWhitelist() 0 4 2
A __construct() 0 13 4
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the box project.
7
 *
8
 * (c) Kevin Herrera <[email protected]>
9
 *     Théo Fidry <[email protected]>
10
 *
11
 * This source file is subject to the MIT license that is bundled
12
 * with this source code in the file LICENSE.
13
 */
14
15
namespace KevinGH\Box\Compactor;
16
17
use function array_reduce;
18
use Humbug\PhpScoper\Whitelist;
19
use KevinGH\Box\PhpScoper\Scoper;
20
21
/**
22
 * @private
23
 */
24
final class Compactors
25
{
26
    private $compactors;
27
    private $scoperCompactor;
28
29
    public function __construct(Compactor ...$compactors)
30
    {
31
        $this->compactors = $compactors;
32
33
        foreach ($compactors as $compactor) {
34
            if ($compactor instanceof CompactorProxy) {
35
                $compactor = $compactor->getCompactor();
36
            }
37
38
            if ($compactor instanceof PhpScoper) {
39
                $this->scoperCompactor = $compactor;
40
41
                break;
42
            }
43
        }
44
    }
45
46
    public function compactContents(string $file, string $contents): string
47
    {
48
        return (string) array_reduce(
49
            $this->compactors,
50
            static function (string $contents, Compactor $compactor) use ($file): string {
51
                return $compactor->compact($file, $contents);
52
            },
53
            $contents
54
        );
55
    }
56
57
    public function getScoper(): ?Scoper
58
    {
59
        return null !== $this->scoperCompactor ? $this->scoperCompactor->getScoper() : null;
60
    }
61
62
    public function getScoperWhitelist(): ?Whitelist
63
    {
64
        return null !== $this->scoperCompactor ? $this->scoperCompactor->getScoper()->getWhitelist() : null;
65
    }
66
67
    public function registerWhitelist(Whitelist $whitelist): void
68
    {
69
        if (null !== $this->scoperCompactor) {
70
            $this->scoperCompactor->getScoper()->changeWhitelist($whitelist);
71
        }
72
    }
73
74
    public function toArray(): array
75
    {
76
        return $this->compactors;
77
    }
78
}
79