Passed
Pull Request — master (#407)
by Kirill
05:31
created

CloudFrontResolver::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 5
nc 1
nop 3
dl 0
loc 9
rs 10
c 1
b 0
f 1
1
<?php
2
3
/**
4
 * This file is part of Spiral Framework package.
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace Spiral\Distribution\Resolver;
13
14
use Aws\CloudFront\UrlSigner;
15
use Psr\Http\Message\UriInterface;
16
use Spiral\Distribution\Internal\AmazonUriFactory;
17
use Spiral\Distribution\Internal\DateTimeIntervalFactoryInterface;
18
19
/**
20
 * Amazon CloudFront is a content delivery network (CDN) service.
21
 *
22
 * @psalm-import-type DateIntervalFormat from DateTimeIntervalFactoryInterface
23
 * @see DateTimeIntervalFactoryInterface
24
 */
25
class CloudFrontResolver extends ExpirationAwareResolver
26
{
27
    /**
28
     * @var string
29
     */
30
    private $domain;
31
32
    /**
33
     * @var UrlSigner
34
     */
35
    private $signer;
36
37
    /**
38
     * @var AmazonUriFactory
39
     */
40
    private $factory;
41
42
    /**
43
     * @param string $keyPairId
44
     * @param string $privateKey
45
     * @param string $domain
46
     */
47
    public function __construct(string $keyPairId, string $privateKey, string $domain)
48
    {
49
        $this->assertCloudFrontAvailable();
50
51
        $this->domain = $domain;
52
        $this->factory = new AmazonUriFactory();
53
        $this->signer = new UrlSigner($keyPairId, $privateKey);
54
55
        parent::__construct();
56
    }
57
58
    /**
59
     * @param string $file
60
     * @param DateIntervalFormat|null $expiration
0 ignored issues
show
Bug introduced by
The type Spiral\Distribution\Resolver\DateIntervalFormat was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
61
     * @return UriInterface
62
     * @throws \Exception
63
     */
64
    public function resolve(string $file, $expiration = null): UriInterface
65
    {
66
        $date = $this->getExpirationDateTime($expiration);
67
        $url = $this->signer->getSignedUrl($this->createUrl($file), $date->getTimestamp());
68
69
        return $this->factory->createUri()
70
            ->withScheme('https')
71
            ->withHost($this->domain)
72
            ->withPath($url)
73
        ;
74
    }
75
76
    /**
77
     * @return void
78
     */
79
    protected function assertCloudFrontAvailable(): void
80
    {
81
        if (\class_exists(UrlSigner::class)) {
82
            return;
83
        }
84
85
        throw new \DomainException('AWS SDK not available. Please install "aws/aws-sdk-php" package');
86
    }
87
88
    /**
89
     * @param string $file
90
     * @return string
91
     */
92
    private function createUrl(string $file): string
93
    {
94
        return \sprintf('rtmp://%s/%s', $this->domain, \trim($file, '/'));
95
    }
96
}
97