Redtube   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 10

Importance

Changes 0
Metric Value
wmc 2
lcom 0
cbo 10
dl 0
loc 80
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A prepareSerializer() 0 23 1
A __construct() 0 13 1
1
<?php
2
3
namespace Realshadow\Redtube;
4
5
use Doctrine\Common\Annotations\AnnotationRegistry;
6
use GuzzleHttp\Client;
7
use Illuminate\Support\Collection;
8
use JMS\Serializer\Context;
9
use JMS\Serializer\GraphNavigator;
10
use JMS\Serializer\Handler\HandlerRegistry;
11
use JMS\Serializer\SerializerBuilder;
12
use JMS\Serializer\VisitorInterface;
13
use Realshadow\Redtube\Endpoints\CategoryEndpoint;
14
use Realshadow\Redtube\Endpoints\StarEndpoint;
15
use Realshadow\Redtube\Endpoints\TagEndpoint;
16
use Realshadow\Redtube\Endpoints\VideoEndpoint;
17
use Realshadow\Redtube\Entities\Video;
18
use Realshadow\Redtube\Entities\Videos;
19
20
21
/**
22
 * Redtube API client
23
 *
24
 * @package Realshadow\Redtube
25
 * @author Lukáš Homza <[email protected]>
26
 */
27
class Redtube
28
{
29
30
    /**
31
     * Category endpoint
32
     *
33
     * @var CategoryEndpoint $categories
34
     */
35
    public $categories;
36
37
    /**
38
     * Video endpoint
39
     *
40
     * @var VideoEndpoint $videos
41
     */
42
    public $videos;
43
44
    /**
45
     * Star endpoint
46
     *
47
     * @var StarEndpoint $stars
48
     */
49
    public $stars;
50
51
    /**
52
     * Tag endpoint
53
     *
54
     * @var TagEndpoint $tags
55
     */
56
    public $tags;
57
58
    /**
59
     * Prepare serializer
60
     *
61
     * @return \JMS\Serializer\Serializer
62
     */
63
    private function prepareSerializer()
64
    {
65
        AnnotationRegistry::registerLoader('class_exists');
66
67
        $serializer = SerializerBuilder::create()
68
            ->setDebug(false)
69
            ->addDefaultHandlers()
70
            ->addDefaultListeners()
71
            ->configureHandlers(function (HandlerRegistry $registry) {
72
                $registry->registerHandler(
73
                    GraphNavigator::DIRECTION_DESERIALIZATION,
74
                    Collection::class,
75
                    'xml',
76
                    function (VisitorInterface $visitor, $data, array $type, Context $context) {
77
                        $data = $visitor->visitArray($data, $type, $context);
78
79
                        return new Collection($data);
80
                    }
81
                );
82
            });
83
84
        return $serializer->build();
85
    }
86
87
    /**
88
     * Create new Redtube API client
89
     *
90
     * @param string $host
91
     */
92
    public function __construct($host = 'https://api.redtube.com')
93
    {
94
        $client = new Client([
95
            'base_uri' => $host
96
        ]);
97
98
        $serializer = $this->prepareSerializer();
99
100
        $this->categories = new CategoryEndpoint($client, $serializer);
101
        $this->stars = new StarEndpoint($client, $serializer);
102
        $this->tags = new TagEndpoint($client, $serializer);
103
        $this->videos = new VideoEndpoint($client, $serializer);
104
    }
105
106
}
107