ImageUploader   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 8
dl 0
loc 94
c 0
b 0
f 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
B handle() 0 39 3
prepareImageFile() 0 1 ?
1
<?php
2
3
namespace HustleWorks\Chute;
4
5
use HustleWorks\Chute\DTO\ImageConfiguration;
6
use HustleWorks\Chute\Contracts\ImageValidatorInterface;
7
use HustleWorks\Chute\Contracts\ImageRepositoryInterface;
8
use HustleWorks\Chute\Contracts\StorageInterface;
9
use HustleWorks\Chute\DTO\ImageFile;
10
use HustleWorks\Chute\ServiceResponse;
11
use HustleWorks\Chute\StandardServiceResponse;
12
13
abstract class ImageUploader
14
{
15
    /**
16
     * @var ImageValidatorInterface
17
     */
18
    protected $validator;
19
20
    /**
21
     * @var StorageInterface
22
     */
23
    protected $storage;
24
25
    /**
26
     * @var ImageRepositoryInterface
27
     */
28
    protected $image_repo;
29
30
    /**
31
     * ImageUploader constructor.
32
     *
33
     * @param ImageValidatorInterface  $validator
34
     * @param StorageInterface         $storage
35
     * @param ImageRepositoryInterface $image_repo
36
     */
37
    public function __construct(
38
        ImageValidatorInterface $validator,
39
        StorageInterface $storage,
40
        ImageRepositoryInterface $image_repo
41
    )
42
    {
43
        $this->validator  = $validator;
44
        $this->storage    = $storage;
45
        $this->image_repo = $image_repo;
46
    }
47
48
    /**
49
     * Handle Image Upload
50
     *
51
     * @param mixed              $framework_file
52
     * @param ImageConfiguration $config
53
     * @param array              $optional_data
54
     * @return ServiceResponse
55
     */
56
    public function handle($framework_file, ImageConfiguration $config, array $optional_data = [])
57
    {
58
        /* Create ImageFile object for processing */
59
        $image_file = $this->prepareImageFile($framework_file);
60
61
        /* run validation on upload */
62
        $response = $this->validator->validate($image_file, $config->rules);
63
64
        /* if valid create database entry, store image to disk and generate response */
65
        if ($response->success()) {
66
67
            if ($record = $this->image_repo->findExistingImageable($config->optional_data)) {
68
                $this->image_repo->delete($record);
69
            }
70
71
            $record = $this->image_repo->create([
72
                'name'        => $config->image_name,
73
                'disk'        => $config->temp_disk,
74
                'filename'    => $image_file->filename,
75
                'path'        => $config->directory,
76
                'status'      => 'pending',
77
                'width'       => $image_file->width,
78
                'height'      => $image_file->height,
79
                'mime_type'   => $image_file->mime_type,
80
                'extension'   => $image_file->extension,
81
                'size'        => $image_file->size,
82
                'alt'         => $optional_data['alt'] ?? null,
83
                'title'       => $optional_data['title'] ?? null,
84
                'description' => $optional_data['description'] ?? null,
85
            ]);
86
87
            $this->storage->put($image_file->stream, $record->disk, "$record->path/$record->uuid", $record->filename);
0 ignored issues
show
Documentation introduced by
The property $stream is declared protected in HustleWorks\Chute\DTO\ImageFile. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
Documentation introduced by
The property $disk is declared protected in HustleWorks\Chute\DTO\ImageRecord. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
88
            $response = new StandardServiceResponse(true, [
89
                'image' => $record,
90
            ], 'File successfully uploaded');
91
        }
92
93
        return $response;
94
    }
95
96
97
    /**
98
     * Prepare Image File
99
     *
100
     * Take raw upload and convert it to an image file
101
     *
102
     * @param $framework_file
103
     * @return ImageFile
104
     */
105
    abstract protected function prepareImageFile($framework_file): ImageFile;
106
}