Skip to content
This repository has been archived by the owner on Mar 8, 2018. It is now read-only.

Commit

Permalink
feat(clickcounter-iframe): add command for updating the iframe config
Browse files Browse the repository at this point in the history
  • Loading branch information
coderbyheart committed Dec 11, 2014
1 parent 6b89929 commit a8185b8
Show file tree
Hide file tree
Showing 8 changed files with 196 additions and 0 deletions.
5 changes: 5 additions & 0 deletions app/config/parameters.yml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,8 @@ parameters:
dothiv_business.podio.appToken: 1234
dothiv_business.podio.clientId: 1234
dothiv_business.podio.clientSecret: 1234

# IFrame Appliance
clickcounter_iframe_service_url: http://iframe.clickcounter.hiv
clickcounter_iframe_service_username: iframe-admin
clickcounter_iframe_service_password: changeme
5 changes: 5 additions & 0 deletions app/config/parameters.yml.jenkins
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,8 @@ parameters:
dothiv_business.podio.appToken: 1234
dothiv_business.podio.clientId: 1234
dothiv_business.podio.clientSecret: 1234

# IFrame Appliance
clickcounter_iframe_service_url: http://iframe.clickcounter.hiv
clickcounter_iframe_service_username: iframe-admin
clickcounter_iframe_service_password: changeme
5 changes: 5 additions & 0 deletions app/config/parameters.yml.travis
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,8 @@ parameters:
dothiv_business.podio.appToken: 1234
dothiv_business.podio.clientId: 1234
dothiv_business.podio.clientSecret: 1234

# IFrame Appliance
clickcounter_iframe_service_url: http://iframe.clickcounter.hiv
clickcounter_iframe_service_username: iframe-admin
clickcounter_iframe_service_password: changeme
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

namespace Dothiv\BusinessBundle\Listener;

use Dothiv\BusinessBundle\Event\ClickCounterConfigurationEvent;
use Dothiv\ValueObject\URLValue;
use Guzzle\Http\Client;

/**
* This listener updates the iframe configuration on the CNAME instance.
*/
class ClickCounterIframeConfigListener
{

/**
* @var Client
*/
private $client;

/**
* @var URLValue
*/
private $serviceUrl;

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

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

/**
* @param Client $client
* @param string $password
* @param string $serviceUrl
* @param string $username
*/
public function __construct(Client $client, $serviceUrl, $username, $password)
{
$this->client = $client;
$this->serviceUrl = new URLValue($serviceUrl);
$this->username = $username;
$this->password = $password;
}

/**
* @param ClickCounterConfigurationEvent $event
*/
public function onClickCounterConfiguration(ClickCounterConfigurationEvent $event)
{
$domain = $event->getDomain();
$config = $event->getConfig();

if (!isset($config['redirect_url'])) {
$this->client->delete(
$this->serviceUrl->toScalar() . 'domain/' . $domain->getName(),
null,
null,
array(
'auth' => array($this->username, $this->password)
)
)->send();
} else {
$this->client->put(
$this->serviceUrl->toScalar() . 'domain/' . $domain->getName(),
array('Content-Type' => 'application/json'),
json_encode(array('redirect' => $config['redirect_url']), JSON_UNESCAPED_SLASHES),
array(
'auth' => array($this->username, $this->password)
)
)->send();
}
}
}
11 changes: 11 additions & 0 deletions src/Dothiv/BusinessBundle/Resources/config/listeners.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ parameters:
dothiv_business.listener.create_user_notification_email_change.class: Dothiv\BusinessBundle\Listener\CreateUserNotificationEmailChangeListener
dothiv_business.listener.update_domain_owner_on_user_email_change.class: Dothiv\BusinessBundle\Listener\UpdateDomainOwnerOnUserEmailChangeListener
dothiv_business.listener.hiv_domain_check.class: Dothiv\BusinessBundle\Listener\HivDomainStatusDomainCheckListener
dothiv_business.listener.clickcounter_iframe_config_listener.class: Dothiv\BusinessBundle\Listener\ClickCounterIframeConfigListener

services:
dothiv_business.listener.create_registration:
Expand Down Expand Up @@ -56,3 +57,13 @@ services:
- @dothiv.repository.entity_change
tags:
- { name: dothiv.business.event_listener, event: hiv_domain_status.domain_check, method: onDomainCheck }

dothiv_business.listener.clickcounter_iframe_config_listener:
class: %dothiv_business.listener.clickcounter_iframe_config_listener.class%
arguments:
- @dothiv.http_client
- %clickcounter_iframe_service_url%
- %clickcounter_iframe_service_username%
- %clickcounter_iframe_service_password%
tags:
- { name: dothiv.business.event_listener, event: dothiv.basewebsite.clickcounter.configuration, method: onClickCounterConfiguration }
4 changes: 4 additions & 0 deletions src/Dothiv/BusinessBundle/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ parameters:
registration.class: Dothiv\BusinessBundle\Service\Registration
dothiv.business.service.nonprofitregistration.class: Dothiv\BusinessBundle\Service\NonprofitRegistrationService
dothiv.business.service.whois.class: Dothiv\BusinessBundle\Service\WhoisSocketService
dothiv.http_client.class: Guzzle\Http\Client

services:
clickcounter:
Expand Down Expand Up @@ -74,3 +75,6 @@ services:

whois:
class: %dothiv.business.service.whois.class%

dothiv.http_client:
class: %dothiv.http_client.class%
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php


namespace Dothiv\BusinessBundle\Test\Listener;

use Dothiv\BusinessBundle\Entity\Banner;
use Dothiv\BusinessBundle\Entity\Domain;
use Dothiv\BusinessBundle\Event\ClickCounterConfigurationEvent;
use Dothiv\BusinessBundle\Listener\ClickCounterIframeConfigListener;
use Guzzle\Http\Client;
use Guzzle\Http\Message\EntityEnclosingRequestInterface;
use Guzzle\Plugin\History\HistoryPlugin;
use Guzzle\Plugin\Mock\MockPlugin;

class ClickCounterIframeConfigListenerTest extends \PHPUnit_Framework_TestCase
{

/**
* @var Client|\PHPUnit_Framework_MockObject_MockObject
*/
private $client;

/**
* @test
* @group BusinessBundle
* @group Listener
*/
public function itShouldBeInstantiable()
{
$this->assertInstanceOf('\Dothiv\BusinessBundle\Listener\ClickCounterIframeConfigListener', $this->createTestObject());
}

/**
* @test
* @group BusinessBundle
* @group Listener
* @depends itShouldBeInstantiable
*/
public function itShouldUpdateTheIframeConfig()
{
$domain = new Domain();
$domain->setName('thjnk.hiv');
$banner = new Banner();
$banner->setRedirectUrl('http://www.thjnk.de/');
$domain->setActiveBanner($banner);

// Mock response
$plugin = new MockPlugin();
$plugin->addResponse(__DIR__ . '/data/domain-created.data');
$this->client->addSubscriber($plugin);

$history = new HistoryPlugin();
$this->client->addSubscriber($history);

$event = new ClickCounterConfigurationEvent($domain, array('redirect_url' => $banner->getRedirectUrl()));
$this->createTestObject()->onClickCounterConfiguration($event);

/** @var EntityEnclosingRequestInterface $request */
$request = $history->getLastRequest();
$this->assertEquals($request->getUrl(), 'http://iframe.clickcounter.hiv/domain/thjnk.hiv');
$this->assertEquals($request->getMethod(), 'PUT');
$this->assertEquals($request->getHeader('Content-Type'), 'application/json');
$this->assertEquals($request->getUsername(), 'iframe-admin');
$this->assertEquals($request->getPassword(), 'some-password');
$this->assertEquals((string)$request->getBody(), '{"redirect":"http://www.thjnk.de/"}');
}

/**
* @return ClickCounterIframeConfigListener
*/
protected function createTestObject()
{
return new ClickCounterIframeConfigListener($this->client, 'http://iframe.clickcounter.hiv', 'iframe-admin', 'some-password');
}

/**
* Test setup
*/
protected function setUp()
{
parent::setUp();
$this->client = new Client();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
HTTP/1.1 201 Created
Location: /domain/thjnk.hiv
Date: Tue, 09 Dec 2014 09:53:14 GMT
Content-Length: 0
Content-Type: text/plain; charset=utf-8

0 comments on commit a8185b8

Please sign in to comment.