VideoPublisherClient::getStream()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 12
rs 9.4285
cc 3
eloc 9
nc 2
nop 1
1
<?php
2
3
namespace VideoPublisher;
4
5
use VideoPublisher\Domain\Stream;
6
use VideoPublisher\Domain\SimpleStream;
7
use VideoPublisher\Connection\Curl\CurlPost;
8
use VideoPublisher\Connection\ConnectionInterface;
9
use VideoPublisher\Authentication\Authentication;
10
use VideoPublisher\Exception\BadResponseException;
11
use VideoPublisher\Exception\StreamNotFoundException;
12
use VideoPublisher\Exception\TokenCacheNotWritableException;
13
use VideoPublisher\Security\TokenVault;
14
use VideoPublisher\Payload\PayloadFactory;
15
16
/**
17
 * Class VideoPublisherClient.
18
 *
19
 * @author Bart Malestein <[email protected]>
20
 */
21
class VideoPublisherClient
22
{
23
    /**
24
     * @var string
25
     */
26
    private $baseUrl;
27
28
    /**
29
     * @var ConnectionInterface - Class responsible for sending of payloads
30
     */
31
    private $connection;
32
33
    /**
34
     * @var PayloadFactory - Factory that can create payloads
35
     */
36
    private $payloadFactory;
37
38
    /**
39
     * @var Authentication - Class responsible for authentication
40
     */
41
    private $authentication;
42
43
    /**
44
     * VideoPublisherClient constructor.
45
     *
46
     * @param Authentication $authentication - Your preferred authentication, default KeyPairAuthentication
47
     * @param string $tokenCacheLocation - Your preferred token location on disk. This can be any folder, but make sure the application has sufficient rights.
48
     * @param string $baseUrl - http://my.videopublisher.io/
49
     * @param ConnectionInterface $connection
50
     *
51
     * @throws TokenCacheNotWritableException
52
     */
53
    public function __construct(Authentication $authentication, $tokenCacheLocation, $baseUrl = 'http://my.videopublisher.io/', ConnectionInterface $connection = null)
54
    {
55
        $this->baseUrl = $baseUrl;
56
        $this->authentication = $authentication;
57
        $this->payloadFactory = new PayloadFactory(rtrim($this->baseUrl, '/'));
58
59
        if ($connection === null) {
60
            $this->connection = new CurlPost();
61
        } else {
62
            $this->connection = $connection;
63
        }
64
65
        $authentication->injectServices($this->connection, new TokenVault($tokenCacheLocation), $this->payloadFactory);
66
    }
67
68
    /**
69
     * Returns a list of al your published streams, with basic information.
70
     *
71
     * @return SimpleStream[]
72
     * @throws BadResponseException
73
     */
74
    public function listStreams()
75
    {
76
        $token = $this->authentication->getToken();
77
        $payload = $this->payloadFactory->get('/api/published');
78
        $payload->setHeader($token->getName(), $token->getValue());
79
        $response = $this->connection->sendPayload($payload);
80
        if ($response->getStatusCode() >= 200 && $response->getStatusCode() < 300) {
81
            $simpleStreams = [];
82
            foreach ($response->getJsonResponse() as $item) {
83
                $simpleStreams[] = new SimpleStream($item);
84
            }
85
86
            return $simpleStreams;
87
        }
88
        throw new BadResponseException;
89
    }
90
91
92
    /**
93
     * Returns a stream, based on Uuid. Contains basic information and playout url / video player
94
     *
95
     * @param string $uuid
96
     * @return Stream
97
     * @throws StreamNotFoundException
98
     */
99
    public function getStream($uuid)
100
    {
101
        $token = $this->authentication->getToken();
102
        $payload = $this->payloadFactory->get(sprintf('/api/publish/%s', $uuid));
103
        $payload->setHeader($token->getName(), $token->getValue());
104
        $response = $this->connection->sendPayload($payload);
105
        if ($response->getStatusCode() >= 200 && $response->getStatusCode() < 300) {
106
            return new Stream($response->getJsonResponse());
107
        } else {
108
            throw new StreamNotFoundException('No stream found with uuid: ' . $uuid);
109
        }
110
    }
111
112
}