Skip to content
This repository has been archived by the owner on Apr 12, 2019. It is now read-only.

Commit

Permalink
Refactor package generation dependencies a little bit.
Browse files Browse the repository at this point in the history
  • Loading branch information
jwage committed Dec 10, 2013
1 parent 7aef9a4 commit 7bacd19
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 171 deletions.
142 changes: 20 additions & 122 deletions src/JWage/APNS/Safari/Package.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@

namespace JWage\APNS\Safari;

use JWage\APNS\Certificate;
use ErrorException;
use ZipArchive;

class Package
{
/**
Expand All @@ -21,16 +17,6 @@ class Package
'website.json'
);

/**
* @var \JWage\APNS\Certificate
*/
private $certificate;

/**
* @var string
*/
private $basePushPackagePath;

/**
* @var string
*/
Expand All @@ -41,137 +27,49 @@ class Package
*/
private $userId;

/**
* @var string
*/
private $host;

/**
* @var string
*/
private $zipPath;

/**
* @var boolean
*/
private $isWritten = false;

/**
* @param \JWage\APNS\Certificate $certificate
* @param string $basePushPackagePath
* @param string $packageDir
* @param string $userId
* @param string $host
*/
public function __construct(
Certificate $certificate,
$basePushPackagePath,
$packageDir,
$userId,
$host
)
public function __construct($packageDir, $userId)
{
$this->certificate = $certificate;
$this->basePushPackagePath = $basePushPackagePath;
$this->packageDir = $packageDir;
$this->userId = $userId;
$this->host = $host;
$this->zipPath = sprintf('%s.zip', $this->packageDir);
$this->zipPath = sprintf('%s.zip', $packageDir);
}

/**
* Gets path to the zip package.
* Gets path to the zip package directory.
*
* @return string $zipPath
* @return string $packageDir
*/
public function getZipPath()
public function getPackageDir()
{
if ($this->isWritten === false) {
$this->generatePackage();
}

return $this->zipPath;
return $this->packageDir;
}

private function generatePackage()
{
$this->isWritten = true;

if (!is_dir($this->packageDir)) {
mkdir($this->packageDir);
}

$this->copyPackageFiles();
$this->createManifest();
$this->createSignature();

$zip = $this->createZipArchive();

if (!$zip->open($this->zipPath, ZipArchive::CREATE)) {
throw new ErrorException(sprintf('Could not open package "%s"', $this->zipPath));
}

$packageFiles = self::$packageFiles;
$packageFiles[] = 'manifest.json';
$packageFiles[] = 'signature';

foreach ($packageFiles as $packageFile) {
$filePath = sprintf('%s/%s', $this->packageDir, $packageFile);

if (!file_exists($filePath)) {
throw new ErrorException(sprintf('File does not exist "%s"', $filePath));
}

$zip->addFile($filePath, $packageFile);
}

if (false === $zip->close()) {
throw new ErrorException(sprintf('Could not save package "%s"', $this->zipPath));
}
}

private function copyPackageFiles()
{
mkdir($this->packageDir . '/icon.iconset');

foreach (Package::$packageFiles as $rawFile) {
$filePath = sprintf('%s/%s', $this->packageDir, $rawFile);

copy(sprintf('%s/%s', $this->basePushPackagePath, $rawFile), $filePath);

if ($rawFile === 'website.json') {
$websiteJson = file_get_contents($filePath);
$websiteJson = str_replace('{{ userId }}', $this->userId, $websiteJson);
$websiteJson = str_replace('{{ host }}', $this->host, $websiteJson);
file_put_contents($filePath, $websiteJson);
}
}
}

private function createManifest()
{
return $this->createPackageManifest()->createManifest();
}

private function createSignature()
{
return $this->createPackageSigner()->createPackageSignature(
$this->certificate, $this->packageDir
);
}

protected function createPackageSigner()
{
return new PackageSigner();
}

protected function createPackageManifest()
/**
* Gets the user id this package is for.
*
* @return string $userId
*/
public function getUserId()
{
return new PackageManifest($this->packageDir);
return $this->userId;
}

protected function createZipArchive()
/**
* Gets path to the zip package.
*
* @return string $zipPath
*/
public function getZipPath()
{
return new ZipArchive();
return $this->zipPath;
}
}
99 changes: 91 additions & 8 deletions src/JWage/APNS/Safari/PackageGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,100 @@ public function __construct(Certificate $certificate, $basePushPackagePath, $hos
public function createPushPackageForUser($userId)
{
$packageDir = sprintf('/%s/pushPackage%s.%s', sys_get_temp_dir(), time(), $userId);
return $this->createPackage($packageDir, $userId);
$package = $this->createPackage($packageDir, $userId);

$this->generatePackage($package);

return $package;
}

protected function createPackage($packageDir, $userId)
private function generatePackage(Package $package)
{
$packageDir = $package->getPackageDir();
$zipPath = $package->getZipPath();

if (!is_dir($packageDir)) {
mkdir($packageDir);
}

$this->copyPackageFiles($package);
$this->createPackageManifest($package);
$this->createPackageSignature($package);

$zip = $this->createZipArchive();

if (!$zip->open($zipPath, ZipArchive::CREATE)) {
throw new ErrorException(sprintf('Could not open package "%s"', $zipPath));
}

$packageFiles = Package::$packageFiles;
$packageFiles[] = 'manifest.json';
$packageFiles[] = 'signature';

foreach ($packageFiles as $packageFile) {
$filePath = sprintf('%s/%s', $packageDir, $packageFile);

if (!file_exists($filePath)) {
throw new ErrorException(sprintf('File does not exist "%s"', $filePath));
}

$zip->addFile($filePath, $packageFile);
}

if (false === $zip->close()) {
throw new ErrorException(sprintf('Could not save package "%s"', $zipPath));
}
}

private function copyPackageFiles(Package $package)
{
$packageDir = $package->getPackageDir();

mkdir($packageDir . '/icon.iconset');

foreach (Package::$packageFiles as $rawFile) {
$filePath = sprintf('%s/%s', $packageDir, $rawFile);

copy(sprintf('%s/%s', $this->basePushPackagePath, $rawFile), $filePath);

if ($rawFile === 'website.json') {
$websiteJson = file_get_contents($filePath);
$websiteJson = str_replace('{{ userId }}', $package->getUserId(), $websiteJson);
$websiteJson = str_replace('{{ host }}', $this->host, $websiteJson);
file_put_contents($filePath, $websiteJson);
}
}
}

private function createPackageManifest(Package $package)
{
return $this->createPackageManifester()->createManifest($package);
}

private function createPackageSignature(Package $package)
{
return new Package(
$this->certificate,
$this->basePushPackagePath,
$packageDir,
$userId,
$this->host
return $this->createPackageSigner()->createPackageSignature(
$this->certificate, $package
);
}

protected function createPackageSigner()
{
return new PackageSigner();
}

protected function createPackageManifester()
{
return new PackageManifester();
}

protected function createZipArchive()
{
return new ZipArchive();
}

protected function createPackage($packageDir, $userId)
{
return new Package($packageDir, $userId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,23 @@

namespace JWage\APNS\Safari;

class PackageManifest
class PackageManifester
{
/**
* @var string
*/
private $packageDir;

/**
* Construct.
*
* @param string $packageDir
*/
public function __construct($packageDir)
{
$this->packageDir = $packageDir;
}

/**
* Generates a manifest JSON file and returns the path.
*
* @param \JWage\APNS\Safari\Package $package
* @return string $manifestJsonPath
*/
public function createManifest()
public function createManifest(Package $package)
{
$manifestData = array();
foreach (Package::$packageFiles as $rawFile) {
$filePath = sprintf('%s/%s', $this->packageDir, $rawFile);
$filePath = sprintf('%s/%s', $package->getPackageDir(), $rawFile);
$manifestData[$rawFile] = sha1(file_get_contents($filePath));
}

$manifestJsonPath = sprintf('%s/manifest.json', $this->packageDir);
$manifestJsonPath = sprintf('%s/manifest.json', $package->getPackageDir());
$manifestJson = json_encode((object) $manifestData);

file_put_contents($manifestJsonPath, $manifestJson);
Expand Down
8 changes: 4 additions & 4 deletions src/JWage/APNS/Safari/PackageSigner.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ class PackageSigner
* Creates a package signature using the given certificate and package directory.
*
* @param \JWage\APNS\Certificate $certificate
* @param string $packageDir
* @param \JWage\APNS\Safari\Package $package
*/
public function createPackageSignature(Certificate $certificate, $packageDir)
public function createPackageSignature(Certificate $certificate, Package $package)
{
$pkcs12 = $certificate->getCertificateString();
$certPassword = $certificate->getPassword();
Expand All @@ -25,8 +25,8 @@ public function createPackageSignature(Certificate $certificate, $packageDir)
throw new RuntimeException('Failed to create signature.');
}

$signaturePath = sprintf('%s/signature', $packageDir);
$manifestJsonPath = sprintf('%s/manifest.json', $packageDir);
$signaturePath = sprintf('%s/signature', $package->getPackageDir());
$manifestJsonPath = sprintf('%s/manifest.json', $package->getPackageDir());

// Sign the manifest.json file with the private key from the certificate
$certData = openssl_x509_read($certs['cert']);
Expand Down
22 changes: 4 additions & 18 deletions tests/JWage/APNS/Tests/Safari/PackageGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,33 +79,19 @@ public function testCreatePackageForUser()
}
}

class PackageStub extends Package
{
protected function createPackageSigner()
{
return new PackageSignerStub();
}
}

class PackageSignerStub extends PackageSigner
{
public function createPackageSignature(Certificate $certificate, $packageDir)
public function createPackageSignature(Certificate $certificate, Package $package)
{
$signaturePath = sprintf('%s/signature', $packageDir);
$signaturePath = sprintf('%s/signature', $package->getPackageDir());
file_put_contents($signaturePath, 'test signature');
}
}

class PackageGeneratorStub extends PackageGenerator
{
protected function createPackage($packageDir, $userId)
protected function createPackageSigner()
{
return new PackageStub(
$this->certificate,
$this->basePushPackagePath,
$packageDir,
$userId,
$this->host
);
return new PackageSignerStub();
}
}

0 comments on commit 7bacd19

Please sign in to comment.