Skip to content

Commit dba2f9f

Browse files
committed
Merge pull request #65 from wtfzdotnet/feature-event-dispatcher-factories
Feature event dispatcher factories
2 parents 7b465d0 + 04ff0e1 commit dba2f9f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+535
-131
lines changed

examples/movies/model/rate.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
require_once '../../../apikey.php';
1515

1616
$token = new \Tmdb\ApiToken(TMDB_API_KEY);
17-
$client = new \Tmdb\Client($token);
17+
$client = new \Tmdb\Client($token, ['session_token' => new \Tmdb\GuestSessionToken(TMDB_GUEST_SESSION_TOKEN), 'log' => ['enabled' => true, 'handler' => new \Monolog\Handler\ChromePHPHandler()]]);
1818

1919
/**
2020
$sessionToken = new \Tmdb\SessionToken(TMDB_SESSION_TOKEN);

examples/tv/model/tv/latest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
* @copyright (c) 2013, Michael Roterman
1111
* @version 0.0.1
1212
*/
13-
require_once '../../../vendor/autoload.php';
14-
require_once '../../../apikey.php';
13+
require_once '../../../../vendor/autoload.php';
14+
require_once '../../../../apikey.php';
1515

1616
$token = new \Tmdb\ApiToken(TMDB_API_KEY);
1717
$client = new \Tmdb\Client($token);

lib/Tmdb/Client.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,16 @@ public function getAdapter()
124124
return $this->options['adapter'];
125125
}
126126

127+
/**
128+
* Get the event dispatcher
129+
*
130+
* @return AdapterInterface
131+
*/
132+
public function getEventDispatcher()
133+
{
134+
return $this->options['event_dispatcher'];
135+
}
136+
127137
/**
128138
* @return array
129139
*/

lib/Tmdb/Event/HydrationEvent.php

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
<?php
2+
/**
3+
* This file is part of the Tmdb PHP API created by Michael Roterman.
4+
*
5+
* For the full copyright and license information, please view the LICENSE
6+
* file that was distributed with this source code.
7+
*
8+
* @package Tmdb
9+
* @author Michael Roterman <[email protected]>
10+
* @copyright (c) 2013, Michael Roterman
11+
* @version 0.0.1
12+
*/
13+
namespace Tmdb\Event;
14+
15+
use Symfony\Component\EventDispatcher\Event;
16+
use Tmdb\HttpClient\Request;
17+
use Tmdb\HttpClient\Response;
18+
use Tmdb\Model\AbstractModel;
19+
20+
class HydrationEvent extends Event
21+
{
22+
/**
23+
* @var AbstractModel
24+
*/
25+
private $subject;
26+
27+
/**
28+
* @var array
29+
*/
30+
private $data;
31+
32+
/**
33+
* @var Request|null
34+
*/
35+
private $lastRequest;
36+
37+
/**
38+
* @var Response|null
39+
*/
40+
private $lastResponse;
41+
42+
/**
43+
* Constructor
44+
*
45+
* @param AbstractModel $subject
46+
* @param array $data
47+
*/
48+
public function __construct(AbstractModel $subject, array $data = [])
49+
{
50+
$this->subject = $subject;
51+
$this->data = $data;
52+
}
53+
54+
/**
55+
* @return AbstractModel
56+
*/
57+
public function getSubject()
58+
{
59+
return $this->subject;
60+
}
61+
62+
/**
63+
* @param AbstractModel $subject
64+
* @return $this
65+
*/
66+
public function setSubject($subject)
67+
{
68+
$this->subject = $subject;
69+
70+
return $this;
71+
}
72+
73+
/**
74+
* @return array
75+
*/
76+
public function getData()
77+
{
78+
return $this->data;
79+
}
80+
81+
/**
82+
* @param array $data
83+
* @return $this
84+
*/
85+
public function setData($data)
86+
{
87+
$this->data = $data;
88+
89+
return $this;
90+
}
91+
92+
/**
93+
* @return bool
94+
*/
95+
public function hasData()
96+
{
97+
return !empty($this->data);
98+
}
99+
100+
/**
101+
* @return Request
102+
*/
103+
public function getLastRequest()
104+
{
105+
return $this->lastRequest;
106+
}
107+
108+
/**
109+
* @param Request|null $lastRequest
110+
* @return $this
111+
*/
112+
public function setLastRequest($lastRequest)
113+
{
114+
$this->lastRequest = $lastRequest;
115+
116+
return $this;
117+
}
118+
119+
/**
120+
* @return Response
121+
*/
122+
public function getLastResponse()
123+
{
124+
return $this->lastResponse;
125+
}
126+
127+
/**
128+
* @param Response|null $lastResponse
129+
* @return $this
130+
*/
131+
public function setLastResponse($lastResponse)
132+
{
133+
$this->lastResponse = $lastResponse;
134+
135+
return $this;
136+
}
137+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
/**
3+
* This file is part of the Tmdb PHP API created by Michael Roterman.
4+
*
5+
* For the full copyright and license information, please view the LICENSE
6+
* file that was distributed with this source code.
7+
*
8+
* @package Tmdb
9+
* @author Michael Roterman <[email protected]>
10+
* @copyright (c) 2013, Michael Roterman
11+
* @version 0.0.1
12+
*/
13+
namespace Tmdb\Event;
14+
15+
use Tmdb\Common\ObjectHydrator;
16+
use Tmdb\HttpClient\HttpClientEventSubscriber;
17+
18+
/**
19+
* Class RequestSubscriber
20+
* @package Tmdb\Event
21+
*/
22+
class HydrationSubscriber extends HttpClientEventSubscriber
23+
{
24+
/**
25+
* Get subscribed events
26+
*
27+
* @return array
28+
*/
29+
public static function getSubscribedEvents()
30+
{
31+
return [
32+
TmdbEvents::HYDRATE => 'hydrate',
33+
];
34+
}
35+
36+
/**
37+
* Hydrate the subject with data
38+
*
39+
* @param HydrationEvent $event
40+
* @return \Tmdb\Model\AbstractModel
41+
*/
42+
public function hydrate(HydrationEvent $event)
43+
{
44+
// Possibility to load serialized cache
45+
$event->getDispatcher()->dispatch(TmdbEvents::BEFORE_HYDRATION, $event);
46+
47+
if ($event->isPropagationStopped()) {
48+
return $event->getSubject();
49+
}
50+
51+
$subject = $this->hydrateSubject($event);
52+
$event->setSubject($subject);
53+
54+
// Possibility to cache the data
55+
$event->getDispatcher()->dispatch(TmdbEvents::AFTER_HYDRATION, $event);
56+
57+
return $event->getSubject();
58+
}
59+
60+
/**
61+
* Hydrate the subject
62+
*
63+
* @param HydrationEvent $event
64+
* @return \Tmdb\Model\AbstractModel
65+
*/
66+
public function hydrateSubject(HydrationEvent $event)
67+
{
68+
$objectHydrator = new ObjectHydrator();
69+
70+
return $objectHydrator->hydrate($event->getSubject(), $event->getData());
71+
}
72+
}

lib/Tmdb/Event/TmdbEvents.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,13 @@
1414

1515
final class TmdbEvents
1616
{
17+
/** Request */
1718
const BEFORE_REQUEST = 'tmdb.before_request';
1819
const REQUEST = 'tmdb.request';
1920
const AFTER_REQUEST = 'tmdb.after_request';
21+
22+
/** Hydration */
23+
const BEFORE_HYDRATION = 'tmdb.before_hydration';
24+
const HYDRATE = 'tmdb.hydrate';
25+
const AFTER_HYDRATION = 'tmdb.after_hydration';
2026
}

lib/Tmdb/Factory/AbstractFactory.php

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
*/
1313
namespace Tmdb\Factory;
1414

15-
use Tmdb\Common\ObjectHydrator;
15+
use Tmdb\Event\HydrationEvent;
16+
use Tmdb\Event\TmdbEvents;
17+
use Tmdb\HttpClient\HttpClient;
1618
use Tmdb\Model\AbstractModel;
1719
use Tmdb\Model\Collection\ResultCollection;
1820
use Tmdb\Model\Common\AccountStates;
@@ -26,6 +28,21 @@
2628
*/
2729
abstract class AbstractFactory
2830
{
31+
/**
32+
* @var HttpClient
33+
*/
34+
protected $httpClient;
35+
36+
/**
37+
* Constructor
38+
*
39+
* @param HttpClient $httpClient
40+
*/
41+
public function __construct(HttpClient $httpClient)
42+
{
43+
$this->httpClient = $httpClient;
44+
}
45+
2946
/**
3047
* Convert an array to an hydrated object
3148
*
@@ -42,6 +59,16 @@ abstract public function create(array $data = []);
4259
*/
4360
abstract public function createCollection(array $data = []);
4461

62+
/**
63+
* Get the http client
64+
*
65+
* @return HttpClient
66+
*/
67+
protected function getHttpClient()
68+
{
69+
return $this->httpClient;
70+
}
71+
4572
/**
4673
* Create a generic collection of data and map it on the class by it's static parameter $properties
4774
*
@@ -179,14 +206,20 @@ public function createResult(array $data = [])
179206
/**
180207
* Hydrate the object with data
181208
*
182-
* @param AbstractModel $object
209+
* @param AbstractModel $subject
183210
* @param array $data
184211
* @return AbstractModel
185212
*/
186-
protected function hydrate(AbstractModel $object, $data = [])
213+
protected function hydrate(AbstractModel $subject, $data = [])
187214
{
188-
$objectHydrator = new ObjectHydrator();
215+
$httpClient = $this->getHttpClient();
216+
217+
$event = new HydrationEvent($subject, $data);
218+
$event->setLastRequest($httpClient->getLastRequest());
219+
$event->setLastResponse($httpClient->getLastResponse());
220+
221+
$this->getHttpClient()->getEventDispatcher()->dispatch(TmdbEvents::HYDRATE, $event);
189222

190-
return $objectHydrator->hydrate($object, $data);
223+
return $event->getSubject();
191224
}
192225
}

lib/Tmdb/Factory/AccountFactory.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
*/
1313
namespace Tmdb\Factory;
1414

15+
use Tmdb\HttpClient\HttpClient;
1516
use Tmdb\Model\Account;
1617
use Tmdb\Model\Lists\Result;
1718

@@ -36,11 +37,18 @@ class AccountFactory extends AbstractFactory
3637
*/
3738
private $tvFactory;
3839

39-
public function __construct()
40+
/**
41+
* Constructor
42+
*
43+
* @param HttpClient $httpClient
44+
*/
45+
public function __construct(HttpClient $httpClient)
4046
{
41-
$this->movieFactory = new MovieFactory();
42-
$this->imageFactory = new ImageFactory();
43-
$this->tvFactory = new TvFactory();
47+
$this->movieFactory = new MovieFactory($httpClient);
48+
$this->imageFactory = new ImageFactory($httpClient);
49+
$this->tvFactory = new TvFactory($httpClient);
50+
51+
parent::__construct($httpClient);
4452
}
4553

4654
/**

lib/Tmdb/Factory/CollectionFactory.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
*/
1313
namespace Tmdb\Factory;
1414

15+
use Tmdb\HttpClient\HttpClient;
1516
use Tmdb\Model\Collection;
1617
use Tmdb\Model\Common\GenericCollection;
1718

@@ -33,11 +34,15 @@ class CollectionFactory extends AbstractFactory
3334

3435
/**
3536
* Constructor
37+
*
38+
* @param HttpClient $httpClient
3639
*/
37-
public function __construct()
40+
public function __construct(HttpClient $httpClient)
3841
{
39-
$this->movieFactory = new MovieFactory();
40-
$this->imageFactory = new ImageFactory();
42+
$this->movieFactory = new MovieFactory($httpClient);
43+
$this->imageFactory = new ImageFactory($httpClient);
44+
45+
parent::__construct($httpClient);
4146
}
4247

4348
/**

0 commit comments

Comments
 (0)