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 <michael@wtfz.net>
10 * @copyright (c) 2013, Michael Roterman
11 * @version 0.0.1
12 */
13namespace Tmdb\Factory;
14
15use Tmdb\Exception\NotImplementedException;
16use Tmdb\HttpClient\HttpClient;
17use Tmdb\Model\Genre;
18use Tmdb\Model\Credits as Credits;
19use Tmdb\Model\Person;
20
21/**
22 * Class CreditsFactory
23 * @package Tmdb\Factory
24 */
25class CreditsFactory extends AbstractFactory
26{
27    /**
28     * @var TvSeasonFactory
29     */
30    private $tvSeasonFactory;
31
32    /**
33     * @var TvEpisodeFactory
34     */
35    private $tvEpisodeFactory;
36
37    /**
38     * @var PeopleFactory
39     */
40    private $peopleFactory;
41
42    /**
43     * Constructor
44     *
45     * @param HttpClient $httpClient
46     */
47    public function __construct(HttpClient $httpClient)
48    {
49        $this->tvSeasonFactory  = new TvSeasonFactory($httpClient);
50        $this->tvEpisodeFactory = new TvEpisodeFactory($httpClient);
51        $this->peopleFactory    = new PeopleFactory($httpClient);
52
53        parent::__construct($httpClient);
54    }
55
56    /**
57     * @param array $data
58     *
59     * @return Genre
60     */
61    public function create(array $data = [])
62    {
63        $credits = new Credits();
64
65        if (array_key_exists('media', $data)) {
66
67            $credits->setMedia(
68                $this->hydrate($credits->getMedia(), $data['media'])
69            );
70
71            if (array_key_exists('seasons', $data['media'])) {
72                $episodes = $this->getTvSeasonFactory()->createCollection($data['media']['seasons']);
73                $credits->getMedia()->setSeasons($episodes);
74            }
75
76            if (array_key_exists('episodes', $data['media'])) {
77                $episodes = $this->getTvEpisodeFactory()->createCollection($data['media']['episodes']);
78                $credits->getMedia()->setEpisodes($episodes);
79            }
80        }
81
82        if (array_key_exists('person', $data)) {
83            $person = $this->getPeopleFactory()->create($data['person']);
84
85            if ($person instanceof Person) {
86                $credits->setPerson($person);
87            }
88        }
89
90        return $this->hydrate($credits, $data);
91    }
92
93    /**
94     * @throws NotImplementedException
95     */
96    public function createCollection(array $data = [])
97    {
98        throw new NotImplementedException(
99            'Credits are usually obtained through the PeopleFactory,
100            however we might add a shortcut for that here.'
101        );
102    }
103
104    /**
105     * @param  \Tmdb\Factory\TvEpisodeFactory $tvEpisodeFactory
106     * @return $this
107     */
108    public function setTvEpisodeFactory($tvEpisodeFactory)
109    {
110        $this->tvEpisodeFactory = $tvEpisodeFactory;
111
112        return $this;
113    }
114
115    /**
116     * @return \Tmdb\Factory\TvEpisodeFactory
117     */
118    public function getTvEpisodeFactory()
119    {
120        return $this->tvEpisodeFactory;
121    }
122
123    /**
124     * @param  \Tmdb\Factory\TvSeasonFactory $tvSeasonFactory
125     * @return $this
126     */
127    public function setTvSeasonFactory($tvSeasonFactory)
128    {
129        $this->tvSeasonFactory = $tvSeasonFactory;
130
131        return $this;
132    }
133
134    /**
135     * @return \Tmdb\Factory\TvSeasonFactory
136     */
137    public function getTvSeasonFactory()
138    {
139        return $this->tvSeasonFactory;
140    }
141
142    /**
143     * @param  \Tmdb\Factory\PeopleFactory $peopleFactory
144     * @return $this
145     */
146    public function setPeopleFactory($peopleFactory)
147    {
148        $this->peopleFactory = $peopleFactory;
149
150        return $this;
151    }
152
153    /**
154     * @return \Tmdb\Factory\PeopleFactory
155     */
156    public function getPeopleFactory()
157    {
158        return $this->peopleFactory;
159    }
160}
161