Passed
Push — master ( 4c5a84...2a9757 )
by Petr
02:30
created

ImageUpload::process()   B

Complexity

Conditions 7
Paths 12

Size

Total Lines 34
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 7

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 16
c 1
b 0
f 0
nc 12
nop 4
dl 0
loc 34
ccs 16
cts 16
cp 1
crap 7
rs 8.8333
1
<?php
2
3
namespace kalanis\kw_images\Content;
4
5
6
use kalanis\kw_files\FilesException;
7
use kalanis\kw_images\Configs;
8
use kalanis\kw_images\Graphics;
9
use kalanis\kw_images\ImagesException;
10
use kalanis\kw_images\Interfaces\ISizes;
11
use kalanis\kw_images\Sources;
12
use kalanis\kw_mime\MimeException;
13
use kalanis\kw_paths\Interfaces\IPaths;
14
use kalanis\kw_paths\PathsException;
15
use kalanis\kw_paths\Stuff;
16
17
18
/**
19
 * Class ImageUpload
20
 * Process uploaded content
21
 * @package kalanis\kw_images\Content
22
 */
23
class ImageUpload
24
{
25
    protected Graphics $graphics;
26
    protected Sources\Image $imageSource;
27
    protected ISizes $config;
28
    protected Images $images;
29
    protected Configs\ProcessorConfig $procConfig;
30
    protected ImageOrientate $orientate;
31
32 3
    public function __construct(
33
        Graphics $graphics,
34
        Sources\Image $libImage,
35
        ISizes $config,
36
        Images $images,
37
        Configs\ProcessorConfig $procConfig,
38
        ImageOrientate $orientate
39
    )
40
    {
41 3
        $this->graphics = $graphics;
42 3
        $this->imageSource = $libImage;
43 3
        $this->config = $config;
44 3
        $this->images = $images;
45 3
        $this->procConfig = $procConfig;
46 3
        $this->orientate = $orientate;
47 3
    }
48
49
    /**
50
     * @param string[] $wantedPath where we want to store the file
51
     * @param string $name
52
     * @throws FilesException
53
     * @throws PathsException
54
     * @return string
55
     */
56 1
    public function findFreeName(array $wantedPath, string $name): string
57
    {
58 1
        $name = Stuff::canonize($name);
59 1
        $ext = $this->procConfig->canLimitExt() && !empty($this->procConfig->getDefaultExt())
60 1
            ? $this->procConfig->getDefaultExt()
61 1
            : Stuff::fileExt($name);
62 1
        if (0 < mb_strlen($ext)) {
63 1
            $ext = IPaths::SPLITTER_DOT . $ext;
64
        }
65 1
        $fileName = Stuff::fileBase($name);
66 1
        return $this->imageSource->findFreeName($wantedPath, $fileName, $ext);
67
    }
68
69
    /**
70
     * @param string[] $wantedPath where we want to store the file
71
     * @param string $tempPath where the file is accessible after upload
72
     * @param string $description
73
     * @param bool $orientate
74
     * @throws FilesException
75
     * @throws ImagesException
76
     * @throws MimeException
77
     * @throws PathsException
78
     * @return bool
79
     */
80 1
    public function process(array $wantedPath, string $tempPath = '', string $description = '', bool $orientate = false): bool
81
    {
82 1
        $fullPath = array_values($wantedPath);
83
        // check file
84 1
        $this->graphics->setSizes($this->config)->check($tempPath, $fullPath);
85
86
        // resize if set
87 1
        if ($this->procConfig->canLimitSize() && !empty($this->config->getMaxInWidth()) && !empty($this->config->getMaxInHeight())) {
88 1
            $this->graphics->setSizes($this->config)->resize($tempPath, $fullPath);
89
        }
90
91
        // store image
92 1
        $uploaded = strval(@file_get_contents($tempPath));
93 1
        $this->imageSource->set($fullPath, $uploaded);
94 1
        @unlink($tempPath);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for unlink(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

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

94
        /** @scrutinizer ignore-unhandled */ @unlink($tempPath);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
95
96
        // orientate if set
97 1
        if ($orientate) {
98
            try {
99 1
                $this->orientate->process($fullPath);
100 1
            } catch (ImagesException $ex) {
101
                // this failure will be skipped
102
            }
103
        }
104
105
        // thumbs
106 1
        $this->images->removeThumb($fullPath);
107 1
        if ($this->procConfig->hasThumb()) {
108 1
            $this->images->updateThumb($fullPath);
109
        }
110
111
        // description
112 1
        $this->images->updateDescription($fullPath, $description);
113 1
        return true;
114
    }
115
}
116