Test Failed
Pull Request — master (#6)
by Laurens
02:27
created

Compressor   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 78.56%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 2
dl 0
loc 36
ccs 11
cts 14
cp 0.7856
rs 10
c 0
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\MetaData\Image;
9
use LauLamanApps\ApplePassbook\Passbook;
10
use ZipArchive;
11
12
class Compressor
13
{
14
    public const FILENAME = 'pass.pkpass';
15
16
    private ZipArchive $zipArchive;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING, expecting T_FUNCTION or T_CONST
Loading history...
17
18
    public function __construct(ZipArchive $zipArchive)
19
    {
20
        $this->zipArchive = $zipArchive;
21
    }
22
23
    /**
24
     * @throws ZipException
25
     */
26
    public function compress(Passbook $passbook, string $temporaryDirectory): void
27
    {
28
        $file = $temporaryDirectory . self::FILENAME;
29
30
        if (($errorCode = $this->zipArchive->open($file, ZipArchive::CREATE)) !== true) {
31
            throw ZipException::canNotOpenZip($file, $errorCode);
32
        }
33
34
        $this->zipArchive->addFile($temporaryDirectory . Signer::FILENAME, Signer::FILENAME);
35
        $this->zipArchive->addFile($temporaryDirectory . ManifestGenerator::FILENAME, ManifestGenerator::FILENAME);
36
        $this->zipArchive->addFromString(Compiler::PASS_DATA_FILE, (string) json_encode($passbook->getData()));
37
38
        foreach ($passbook->getImages() as $image) {
39
            /** @var Image $image */
40
            $this->zipArchive->addFile($image->getPath(), $image->getFilename());
41
        }
42
43
        $this->zipArchive->close();
44
    }
45
}
46