PackageGenerator::createPackage()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
3
namespace JWage\APNS\Safari;
4
5
use ErrorException;
6
use JWage\APNS\Certificate;
7
use ZipArchive;
8
9
class PackageGenerator
10
{
11
    /**
12
     * @var \JWage\APNS\Certificate
13
     */
14
    protected $certificate;
15
16
    /**
17
     * @var string
18
     */
19
    protected $basePushPackagePath;
20
21
    /**
22
     * @var string
23
     */
24
    protected $host;
25
26
    /**
27
     * @var string
28
     */
29
    protected $websiteName;
30
31
    /**
32
     * @var string
33
     */
34
    protected $websitePushId;
35
36
    /**
37
     * Construct.
38
     *
39
     * @param \JWage\APNS\Certificate $certificate
40
     * @param string $basePushPackagePath
41
     * @param string $host
42
     * @param string $websiteName
43
     * @param string $websitePushId
44
     */
45
    public function __construct(
46
        Certificate $certificate,
47
        $basePushPackagePath,
48
        $host,
49
        $websiteName = '',
50
        $websitePushId = ''
51
    ) {
52
        $this->certificate = $certificate;
53
        $this->basePushPackagePath = $basePushPackagePath;
54
        $this->host = $host;
55
        $this->websiteName = $websiteName;
56
        $this->websitePushId = $websitePushId;
57
    }
58
59
    /**
60
     * Create a safari website push notification package for the given User.
61
     *
62
     * @param string $userId User id to create package for.
63
     * @return \JWage\APNS\Safari\Package $package Package instance.
64
     */
65
    public function createPushPackageForUser($userId)
66
    {
67
        $packageDir = sprintf('/%s/pushPackage%s.%s', sys_get_temp_dir(), time(), $userId);
68
        $package = $this->createPackage($packageDir, $userId);
69
70
        $this->generatePackage($package);
71
72
        return $package;
73
    }
74
75
    private function generatePackage(Package $package)
76
    {
77
        $packageDir = $package->getPackageDir();
78
        $zipPath = $package->getZipPath();
79
80
        if (!is_dir($packageDir)) {
81
            mkdir($packageDir);
82
        }
83
84
        $this->copyPackageFiles($package);
85
        $this->createPackageManifest($package);
86
        $this->createPackageSignature($package);
87
88
        $zip = $this->createZipArchive();
89
90
        if (!$zip->open($zipPath, ZipArchive::CREATE)) {
91
            throw new ErrorException(sprintf('Could not open package "%s"', $zipPath));
92
        }
93
94
        $packageFiles = Package::$packageFiles;
95
        $packageFiles[] = 'manifest.json';
96
        $packageFiles[] = 'signature';
97
98
        foreach ($packageFiles as $packageFile) {
99
            $filePath = sprintf('%s/%s', $packageDir, $packageFile);
100
101
            if (!file_exists($filePath)) {
102
                throw new ErrorException(sprintf('File does not exist "%s"', $filePath));
103
            }
104
105
            $zip->addFile($filePath, $packageFile);
106
        }
107
108
        if (false === $zip->close()) {
109
            throw new ErrorException(sprintf('Could not save package "%s"', $zipPath));
110
        }
111
    }
112
113
    private function copyPackageFiles(Package $package)
114
    {
115
        $packageDir = $package->getPackageDir();
116
117
        mkdir($packageDir . '/icon.iconset');
118
119
        foreach (Package::$packageFiles as $rawFile) {
120
            $filePath = sprintf('%s/%s', $packageDir, $rawFile);
121
122
            copy(sprintf('%s/%s', $this->basePushPackagePath, $rawFile), $filePath);
123
124
            if ($rawFile === 'website.json') {
125
                $websiteJson = file_get_contents($filePath);
126
                $websiteJson = str_replace('{{ userId }}', $package->getUserId(), $websiteJson);
127
                $websiteJson = str_replace('{{ host }}', $this->host, $websiteJson);
128
                $websiteJson = str_replace('{{ websiteName }}', $this->websiteName, $websiteJson);
129
                $websiteJson = str_replace('{{ websitePushId }}', $this->websitePushId, $websiteJson);
130
                file_put_contents($filePath, $websiteJson);
131
            }
132
        }
133
    }
134
135
    private function createPackageManifest(Package $package)
136
    {
137
        return $this->createPackageManifester()->createManifest($package);
138
    }
139
140
    private function createPackageSignature(Package $package)
141
    {
142
        return $this->createPackageSigner()->createPackageSignature(
143
            $this->certificate, $package
144
        );
145
    }
146
147
    protected function createPackageSigner()
148
    {
149
        return new PackageSigner();
150
    }
151
152
    protected function createPackageManifester()
153
    {
154
        return new PackageManifester();
155
    }
156
157
    protected function createZipArchive()
158
    {
159
        return new ZipArchive();
160
    }
161
162
    /**
163
     * @param string $packageDir
164
     * @param string $userId
165
     */
166
    protected function createPackage($packageDir, $userId)
167
    {
168
        return new Package($packageDir, $userId);
169
    }
170
}
171