Compiler::validate()   A
last analyzed

Complexity

Conditions 5
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 4
nc 3
nop 1
dl 0
loc 8
ccs 0
cts 5
cp 0
crap 30
rs 9.6111
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace LauLamanApps\ApplePassbook\Build;
6
7
use LauLamanApps\ApplePassbook\Build\Exception\ZipException;
8
use LauLamanApps\ApplePassbook\Exception\MissingRequiredDataException;
9
use LauLamanApps\ApplePassbook\Passbook;
10
use Ramsey\Uuid\Uuid;
11
12
class Compiler
13
{
14
    public const PASS_DATA_FILE = 'pass.json';
15
16
    private ManifestGenerator $manifestGenerator;
17
    private Signer $signer;
18
    private Compressor $compressor;
19
    private string $passTypeIdentifier;
20
    private string $teamIdentifier;
21
22
    public function __construct(
23
        ManifestGenerator $manifestGenerator,
24
        Signer $signer,
25
        Compressor $compressor,
26
        ?string $passTypeIdentifier = null,
27
        ?string $teamIdentifier = null
28
    ) {
29
        $this->manifestGenerator = $manifestGenerator;
30
        $this->signer = $signer;
31
        $this->compressor = $compressor;
32
33
        if ($passTypeIdentifier) {
34
            $this->passTypeIdentifier = $passTypeIdentifier;
35
        }
36
37
        if ($teamIdentifier) {
38
            $this->teamIdentifier = $teamIdentifier;
39
        }
40
    }
41
42
    public function setPassTypeIdentifier(string $passTypeIdentifier): void
43
    {
44
        $this->passTypeIdentifier = $passTypeIdentifier;
45
    }
46
47
    public function setTeamIdentifier(string $teamIdentifier): void
48
    {
49
        $this->teamIdentifier = $teamIdentifier;
50
    }
51
52
    /**
53
     * @throws MissingRequiredDataException
54
     * @throws ZipException
55
     */
56
    public function compile(Passbook $passbook): string
57
    {
58
        $this->validate($passbook);
59
60
        if (!$passbook->hasPassTypeIdentifier() && isset($this->passTypeIdentifier)) {
61
            $passbook->setPassTypeIdentifier($this->passTypeIdentifier);
62
        }
63
64
        if (!$passbook->hasTeamIdentifier() && isset($this->teamIdentifier)) {
65
            $passbook->setTeamIdentifier($this->teamIdentifier);
66
        }
67
68
        $temporaryDirectory = $this->createTemporaryDirectory();
69
70
        try {
71
            $this->manifestGenerator->generate($passbook, $temporaryDirectory);
72
            $this->signer->sign($temporaryDirectory);
73
            $this->compressor->compress($passbook, $temporaryDirectory);
74
75
            return (string) file_get_contents($temporaryDirectory . Compressor::FILENAME);
76
        } finally {
77
            $this->cleanup($temporaryDirectory);
78
        }
79
    }
80
81
    /**
82
     * @throws MissingRequiredDataException
83
     */
84
    private function validate(Passbook $passbook): void
85
    {
86
        if (isset($this->passTypeIdentifier) && $passbook->hasPassTypeIdentifier() === false) {
87
            throw new MissingRequiredDataException('PassTypeIdentifier is unknown. Either specify it on the passbook and/or specify it in the compiler.');
88
        }
89
90
        if (isset($this->teamIdentifier) && $passbook->hasTeamIdentifier() === false) {
91
            throw new MissingRequiredDataException('TeamIdentifier is unknown. Either specify it on the passbook and/or specify it in the compiler.');
92
        }
93
    }
94
95
    private function createTemporaryDirectory(): string
96
    {
97
        $dir = sprintf('%s/passbook_%s/', sys_get_temp_dir(), Uuid::uuid4()->toString());
98
99
        mkdir($dir);
100
101
        return $dir;
102
    }
103
104
    private function cleanup(string $temporaryDirectory): void
105
    {
106
        $files = array_diff((array) scandir($temporaryDirectory), ['..', '.']);
107
        foreach ($files as $file) {
108
            unlink($temporaryDirectory . $file);
109
        }
110
111
        rmdir($temporaryDirectory);
112
    }
113
}
114