Passed
Pull Request — 2.x (#729)
by Antonio Carlos
06:06
created

MediasUploaderConfig   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Test Coverage

Coverage 94.12%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 35
c 3
b 0
f 0
dl 0
loc 77
ccs 32
cts 34
cp 0.9412
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A compose() 0 42 2
1
<?php
2
3
namespace A17\Twill\Http\ViewComposers;
4
5
use Illuminate\Config\Repository as Config;
6
use Illuminate\Contracts\View\View;
7
use Illuminate\Routing\UrlGenerator;
8
use Illuminate\Session\Store as SessionStore;
9
10
class MediasUploaderConfig
11
{
12
    /**
13
     * @var UrlGenerator
14
     */
15
    protected $urlGenerator;
16
17
    /**
18
     * @var Config
19
     */
20
    protected $config;
21
22
    /**
23
     * @var SessionStore
24
     */
25
    protected $sessionStore;
26
27
    /**
28
     * @param UrlGenerator $urlGenerator
29
     * @param Config $config
30
     * @param SessionStore $sessionStore
31
     */
32 56
    public function __construct(UrlGenerator $urlGenerator, Config $config, SessionStore $sessionStore)
33
    {
34 56
        $this->urlGenerator = $urlGenerator;
35 56
        $this->config = $config;
36 56
        $this->sessionStore = $sessionStore;
37 56
    }
38
39
    /**
40
     * Binds data to the view.
41
     *
42
     * @param View $view
43
     * @return void
44
     */
45 56
    public function compose(View $view)
46
    {
47 56
        $libraryDisk = $this->config->get('twill.media_library.disk');
48 56
        $endpointType = $this->config->get('twill.media_library.endpoint_type');
49 56
        $allowedExtensions = $this->config->get('twill.media_library.allowed_extensions');
50
51
        // anonymous functions are used to let configuration dictate
52
        // the execution of the appropriate implementation
53
        $endpointByType = [
54 56
            'local' => function () {
55 56
                return $this->urlGenerator->route('admin.media-library.medias.store');
56 56
            },
57 56
            's3' => function () use ($libraryDisk) {
58
                return s3Endpoint($libraryDisk);
59 56
            },
60 56
            'azure' => function () use ($libraryDisk) {
61
                return azureEndpoint($libraryDisk);
62 56
            },
63
        ];
64
65
        $signatureEndpointByType = [
66 56
            'local' => null,
67 56
            's3' => $this->urlGenerator->route('admin.media-library.sign-s3-upload'),
68 56
            'azure' => $this->urlGenerator->route('admin.media-library.sign-azure-upload'),
69
        ];
70
71
        $mediasUploaderConfig = [
72 56
            'endpointType' => $endpointType,
73 56
            'endpoint' => $endpointByType[$endpointType](),
74 56
            'successEndpoint' => $this->urlGenerator->route('admin.media-library.medias.store'),
75 56
            'signatureEndpoint' => $signatureEndpointByType[$endpointType],
76 56
            'endpointBucket' => $this->config->get('filesystems.disks.' . $libraryDisk . '.bucket', 'none'),
77 56
            'endpointRegion' => $this->config->get('filesystems.disks.' . $libraryDisk . '.region', 'none'),
78 56
            'endpointRoot' => $endpointType === 'local' ? '' : $this->config->get('filesystems.disks.' . $libraryDisk . '.root', ''),
79 56
            'accessKey' => $this->config->get('filesystems.disks.' . $libraryDisk . '.key', 'none'),
80 56
            'csrfToken' => $this->sessionStore->token(),
81 56
            'acl' => $this->config->get('twill.media_library.acl'),
82 56
            'filesizeLimit' => $this->config->get('twill.media_library.filesize_limit'),
83 56
            'allowedExtensions' => $allowedExtensions,
84
        ];
85
86 56
        $view->with(compact('mediasUploaderConfig'));
87 56
    }
88
}
89