ImageProcessor   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
B handle() 0 25 2
B _createTransformations() 0 31 2
1
<?php
2
3
4
namespace HustleWorks\Chute;
5
6
use HustleWorks\Chute\DTO\ImageConfiguration;
7
use HustleWorks\Chute\DTO\ImageRecord;
8
use HustleWorks\Chute\Contracts\ImageRepositoryInterface;
9
use HustleWorks\Chute\Contracts\ImageTransformationRepositoryInterface;
10
use HustleWorks\Chute\Contracts\StorageInterface;
11
use HustleWorks\Chute\ImageEditor;
12
13
abstract class ImageProcessor
14
{
15
    /**
16
     * @var StorageInterface
17
     */
18
    private $storage;
19
20
    /**
21
     * @var ImageRepositoryInterface
22
     */
23
    private $image_repo;
24
25
    /**
26
     * @var ImageTransformationRepositoryInterface
27
     */
28
    private $image_transformation_repo;
29
30
    /**
31
     * ImageProcessor constructor.
32
     *
33
     * @param StorageInterface $storage
34
     * @param                  $image_repo
35
     * @param                  $image_transformation_repo
36
     */
37
    public function __construct(
38
        StorageInterface $storage,
39
        ImageRepositoryInterface $image_repo,
40
        ImageTransformationRepositoryInterface $image_transformation_repo
41
    )
42
    {
43
        $this->storage                   = $storage;
44
        $this->image_repo                = $image_repo;
45
        $this->image_transformation_repo = $image_transformation_repo;
46
    }
47
48
    /**
49
     * Handle Processing
50
     *
51
     * @param ImageRecord        $record
52
     * @param ImageConfiguration $config
53
     * @return StandardServiceResponse
54
     */
55
    public function handle(ImageRecord $record, ImageConfiguration $config)
56
    {
57
        /* build path */
58
        $path_to_record = "$record->path/$record->uuid/$record->filename";
59
60
        /* get file from storage */
61
        $image = $this->storage->get($record->disk, $path_to_record);
0 ignored issues
show
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...
62
63
        /* move file to primary storage if not already there */
64
        if ($record->disk !== $config->primary_disk) {
0 ignored issues
show
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...
65
            $this->storage->move($path_to_record, $path_to_record, $record->disk, $config->primary_disk);
0 ignored issues
show
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...
66
            $this->image_repo->update($record, ['disk' => $config->primary_disk]);
67
        }
68
69
        /* create transformation for each size */
70
        $transformation_records = $this->_createTransformations($image, $config, $record);
71
72
        $this->image_repo->update($record, ['status' => 'complete']);
73
74
        return new StandardServiceResponse(
75
            true,
76
            ['image' => $record, 'image_transformations' => $transformation_records],
77
            'Image processing completed successfully'
78
        );
79
    }
80
81
    /**
82
     * Create Transformations
83
     *
84
     * @param                    $image
85
     * @param ImageConfiguration $config
86
     * @param ImageRecord        $image_record
87
     * @return array
88
     */
89
    private function _createTransformations($image, ImageConfiguration $config, ImageRecord $image_record)
90
    {
91
        foreach ($config->sizes as $size) {
92
            /* create filename */
93
            $fn = $size->prefix .
94
                str_replace(".$image_record->extension", '', $image_record->filename) .
95
                "$size->suffix.$image_record->extension";
96
97
            /* initialize fluent image editor */
98
            $editor = new ImageEditor($image);
99
100
            /* create transformation */
101
            $transformed_image = $editor->resize($size->width, $size->height)->setQuality($size->quality)->getStream();
102
103
            /* store on disk */
104
            $size_on_disk = $this->storage->put($transformed_image, $config->primary_disk, "$image_record->path/$image_record->uuid", $fn);
105
106
            /* create record */
107
            $transformation_records[] = $this->image_transformation_repo->create([
0 ignored issues
show
Coding Style Comprehensibility introduced by
$transformation_records was never initialized. Although not strictly required by PHP, it is generally a good practice to add $transformation_records = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
108
                'name'     => $size->name,
109
                'filename' => $fn,
110
                'disk'     => $config->primary_disk,
111
                'width'    => $size->width ?? round($size->height / $image_record->height * $image_record->width),
112
                'height'   => $size->height ?? round($size->width / $image_record->width * $image_record->height),
113
                'quality'  => $size->quality,
114
                'size'     => $size_on_disk,
115
            ], $image_record);
116
        }
117
118
        return $transformation_records ?? [];
119
    }
120
}