HasMediaAsyncTrait   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 14
eloc 49
c 3
b 0
f 1
dl 0
loc 79
ccs 0
cts 49
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A uploadGallery() 0 33 6
A setDefaultMediaItem() 0 16 4
A removeMediaItem() 0 17 4
1
<?php
2
3
namespace ByTIC\MediaLibraryModule\Application\Modules\AbstractModule\Controllers\Traits;
4
5
use ByTIC\MediaLibrary\Exceptions\FileCannotBeAdded;
6
use ByTIC\MediaLibrary\Exceptions\FileCannotBeAdded\FileUnacceptableForCollection;
7
use ByTIC\MediaLibrary\FileAdder\FileAdderFactory;
8
use ByTIC\MediaLibrary\HasMedia\HasMediaTrait;
9
use ByTIC\MediaLibraryModule\Controllers\Traits\Async\Covers;
10
use ByTIC\MediaLibraryModule\Controllers\Traits\Async\Files;
11
use ByTIC\MediaLibraryModule\Controllers\Traits\Async\Gallery;
12
use ByTIC\MediaLibraryModule\Controllers\Traits\Async\Images;
13
use ByTIC\MediaLibraryModule\Controllers\Traits\Async\Logos;
14
use Nip\Http\Request;
15
16
/**
17
 * Class HasMediaAsyncTrait
18
 * @package ByTIC\MediaLibraryModule\Application\Modules\AbstractModule\Controllers\Traits
19
 *
20
 * @method HasMediaTrait getModelFromRequest
21
 * @method Request getRequest
22
 */
23
trait HasMediaAsyncTrait
24
{
25
    use Covers;
26
    use Files;
27
    use Gallery;
28
    use Images;
29
    use Logos;
30
31
    public function uploadGallery()
32
    {
33
        if (!$this->getRequest()->files->has('file') || $this->getRequest()->request->has('action')) {
34
            $this->uploadGalleryImgPicker();
35
36
            return;
37
        }
38
39
        /** @var HasMediaTrait $item */
40
        $item = $this->getModelFromRequest();
41
        $mediaType = $this->getRequest()->get('media_type', 'images');
42
43
        /** @var \ByTIC\MediaLibrary\Validation\Constraints\ImageConstraint $constraint */
44
//        $constraint = $item->getMediaRepository()->getCollection('images')->getConstraint();
45
46
        try {
47
            $adder = FileAdderFactory::create($item, $this->getRequest()->files->get('file'));
48
49
            if ($this->getRequest()->request->has('cropper')) {
50
                parse_str($this->getRequest()->request->get('cropper'), $copperData);
51
                $adder->getMedia()->cropperData = $copperData;
0 ignored issues
show
Bug introduced by
The property cropperData does not seem to exist on ByTIC\MediaLibrary\Media\Media.
Loading history...
52
            }
53
            $adder->toMediaCollection($mediaType);
54
        } catch (FileCannotBeAdded $exception) {
55
            http_response_code(415);
56
            header('Content-Type: application/json; charset=utf-8');
57
            $output = ['error' => $exception->getMessage()];
58
            if ($exception instanceof FileUnacceptableForCollection) {
59
                $output['error'] .= ': ' . $exception->violations->getMessageString();
60
            }
61
            echo json_encode($output);
62
        }
63
        die();
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
64
    }
65
66
    public function setDefaultMediaItem()
67
    {
68
        $item = $this->getModelFromRequest();
69
        $type = $this->getRequest()->get('media_type');
70
        switch ($type) {
71
            case 'images':
72
            case 'covers':
73
            case 'logos':
74
                $item->getMedia($type)->persistDefaultMediaFromName(
75
                    $this->getRequest()->get('media_filename')
76
                );
77
                $this->sendSuccess("Imaginea a fost stabilita ca principala");
0 ignored issues
show
Bug introduced by
It seems like sendSuccess() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

77
                $this->/** @scrutinizer ignore-call */ 
78
                       sendSuccess("Imaginea a fost stabilita ca principala");
Loading history...
78
                break;
79
80
            default:
81
                $this->sendError("Imaginea nu a putut fi stabilita ca principala");
0 ignored issues
show
Bug introduced by
It seems like sendError() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

81
                $this->/** @scrutinizer ignore-call */ 
82
                       sendError("Imaginea nu a putut fi stabilita ca principala");
Loading history...
82
        }
83
    }
84
85
    public function removeMediaItem()
86
    {
87
        $item = $this->getModelFromRequest();
88
        $type = $this->getRequest()->get('media_type');
89
90
        switch ($this->getRequest()->get('media_type')) {
91
            case 'images':
92
            case 'covers':
93
            case 'logos':
94
                $item->getMedia($type)->deleteMediaByKey(
0 ignored issues
show
Bug introduced by
It seems like $type can also be of type null; however, parameter $collectionName of ByTIC\MediaLibrary\HasMe...sMediaTrait::getMedia() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

94
                $item->getMedia(/** @scrutinizer ignore-type */ $type)->deleteMediaByKey(
Loading history...
95
                    $this->getRequest()->get('media_filename')
96
                );
97
98
                $this->sendSuccess("Imaginea a fost stearsa");
99
                break;
100
            default:
101
                $this->sendError("Imaginea nu a putut fi stearsa");
102
        }
103
    }
104
}
105