Passed
Pull Request — master (#6)
by Laurens
01:54
created

Compressor::compress()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 11
cts 11
cp 1
rs 9.6666
c 0
b 0
f 0
cc 3
nc 3
nop 2
crap 3
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 2
    public function __construct(ZipArchive $zipArchive)
19
    {
20 2
        $this->zipArchive = $zipArchive;
21 2
    }
22
23
    /**
24
     * @throws ZipException
25
     */
26 2
    public function compress(Passbook $passbook, string $temporaryDirectory): void
27
    {
28 2
        $file = $temporaryDirectory . self::FILENAME;
29
30 2
        if (($errorCode = $this->zipArchive->open($file, ZipArchive::CREATE)) !== true) {
31 1
            throw ZipException::canNotOpenZip($file, $errorCode);
32
        }
33
34 1
        $this->zipArchive->addFile($temporaryDirectory . Signer::FILENAME, Signer::FILENAME);
35 1
        $this->zipArchive->addFile($temporaryDirectory . ManifestGenerator::FILENAME, ManifestGenerator::FILENAME);
36 1
        $this->zipArchive->addFromString(Compiler::PASS_DATA_FILE, (string) json_encode($passbook->getData()));
37
38 1
        foreach ($passbook->getImages() as $image) {
39
            /** @var Image $image */
40 1
            $this->zipArchive->addFile($image->getPath(), $image->getFilename());
41
        }
42
43 1
        $this->zipArchive->close();
44 1
    }
45
}
46