Skip to content
This repository has been archived by the owner on Jun 6, 2021. It is now read-only.

Feature: Switch credentials in runtime. #152

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ protected function addAndroid()
arrayNode("gcm")->
canBeUnset()->
children()->
scalarNode("api_key")->isRequired()->cannotBeEmpty()->end()->
scalarNode("api_key")->defaultValue("")->end()->
booleanNode("use_multi_curl")->defaultValue(true)->end()->
booleanNode("dry_run")->defaultFalse()->end()->
end()->
Expand Down
32 changes: 32 additions & 0 deletions Service/Notifications.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use RMS\PushNotificationsBundle\Device\Types;
use RMS\PushNotificationsBundle\Message\MessageInterface;
use RMS\PushNotificationsBundle\Service\OS\AndroidGCMNotification;
use RMS\PushNotificationsBundle\Service\OS\AppleNotification;

class Notifications
Expand Down Expand Up @@ -99,4 +100,35 @@ public function setAPNSPemAsString($pemContent, $passphrase) {
$appleNotification->setPemAsString($pemContent, $passphrase);
}
}

/**
* Set Apple Push Notification Service pem as filepath.
* Service won't use pem file passed by config anymore.
*
* @param string $pemPath
* @param string $passphrase
*/
public function setAPNSPemPath($pemPath, $passphrase)
{
if (isset($this->handlers[Types::OS_IOS]) && $this->handlers[Types::OS_IOS] instanceof AppleNotification) {
/** @var AppleNotification $appleNotification */
$appleNotification = $this->handlers[Types::OS_IOS];
$appleNotification->setPemPath($pemPath, $passphrase);
}
}

/**
* Set Android GCM API key.
* Service won't use api key passed by config anymore.
*
* @param string $apiKey
*/
public function setGCMApiKey($apiKey)
{
if (isset($this->handlers[Types::OS_ANDROID_GCM]) && $this->handlers[Types::OS_ANDROID_GCM] instanceof AndroidGCMNotification) {
/** @var AndroidGCMNotification $androidGCMNotification */
$androidGCMNotification = $this->handlers[Types::OS_ANDROID_GCM];
$androidGCMNotification->setApiKey($apiKey);
}
}
}
10 changes: 10 additions & 0 deletions Service/OS/AndroidGCMNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,14 @@ public function getResponses()
{
return $this->responses;
}

/**
* Change Google GCM API key
*
* @param string $apiKey
*/
public function setApiKey($apiKey)
{
$this->apiKey = $apiKey;
}
}
17 changes: 17 additions & 0 deletions Service/OS/AppleNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,23 @@ public function setPemAsString($pemContent, $passphrase) {
$this->closeStreams();
}

/**
* @param string $pemPath
* @param string $passphrase
*/
public function setPemPath($pemPath, $passphrase)
{
if ($this->pemPath === $pemPath && $this->passphrase === $passphrase) {
return;
}

$this->pemPath = $pemPath;
$this->passphrase = $passphrase;

// for new pem will take affect we need to close existing streams which use cached pem
$this->closeStreams();
}

/**
* Called on kernel terminate
*/
Expand Down
7 changes: 3 additions & 4 deletions Tests/DependencyInjection/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,7 @@ public function testNewC2DMIsAllowedWithoutOldBits()
$this->assertEquals("123", $config["android"]["c2dm"]["source"]);
}

/**
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
*/
public function testGCMRequiresAPIKey()
public function testGCMEmptyAPIKey()
{
$arr = array(
array(
Expand All @@ -101,6 +98,8 @@ public function testGCMRequiresAPIKey()
),
);
$config = $this->process($arr);
$this->assertEmpty($config["android"]["gcm"]["api_key"]);
$this->assertFalse($config["android"]["gcm"]["dry_run"]);
}

public function testGCMIsOK()
Expand Down