Completed
Push — master ( 444a85...05292a )
by
unknown
05:31
created

isPurgingOfImgixCacheRequired()   A

Complexity

Conditions 6
Paths 4

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 8
cts 8
cp 1
rs 9.2222
c 0
b 0
f 0
cc 6
nc 4
nop 2
crap 6
1
<?php
2
namespace Aoe\Imgix\TYPO3;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2018 AOE GmbH <[email protected]>
8
 *
9
 *  All rights reserved
10
 *
11
 *  This script is part of the TYPO3 project. The TYPO3 project is
12
 *  free software; you can redistribute it and/or modify
13
 *  it under the terms of the GNU General Public License as published by
14
 *  the Free Software Foundation; either version 3 of the License, or
15
 *  (at your option) any later version.
16
 *
17
 *  The GNU General Public License can be found at
18
 *  http://www.gnu.org/copyleft/gpl.html.
19
 *
20
 *  This script is distributed in the hope that it will be useful,
21
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 *  GNU General Public License for more details.
24
 *
25
 *  This copyright notice MUST APPEAR in all copies of the script!
26
 ***************************************************************/
27
28
use Aoe\Imgix\Domain\Service\ImagePurgeService;
29
use TYPO3\CMS\Core\Resource\File;
30
use TYPO3\CMS\Core\Utility\File\ExtendedFileUtility;
31
use TYPO3\CMS\Core\Utility\File\ExtendedFileUtilityProcessDataHookInterface;
32
use TYPO3\CMS\Core\Utility\GeneralUtility;
33
use TYPO3\CMS\Extbase\Object\ObjectManager;
34
35
class FileUtilityProcessDataHook implements ExtendedFileUtilityProcessDataHookInterface
36
{
37
    /**
38
     * @var Configuration
39
     */
40
    private $configuration;
41
42
    /**
43
     * @var ImagePurgeService
44
     */
45
    private $imagePurgeService;
46
47
    /**
48
     * @param Configuration     $configuration
49
     * @param ImagePurgeService $imagePurgeService
50
     */
51 9
    public function __construct(Configuration $configuration = null, ImagePurgeService $imagePurgeService = null)
52
    {
53 9
        if ($configuration === null || $imagePurgeService === null) {
54
            // TYPO3 doesn't support Dependency-Injection in Hooks - so we must create the objects manually
55
            $objectManager = GeneralUtility::makeInstance(ObjectManager::class);
56
            $configuration = $objectManager->get(Configuration::class);
57
            $imagePurgeService = $objectManager->get(ImagePurgeService::class);
58
        }
59 9
        $this->configuration = $configuration;
60 9
        $this->imagePurgeService = $imagePurgeService;
61 9
    }
62
63
    /**
64
     * @param string              $action
65
     * @param array               $cmdArr
66
     * @param array               $result
67
     * @param ExtendedFileUtility $parentObject
68
     * @return void
69
     * @see \TYPO3\CMS\Core\Utility\File\ExtendedFileUtility::processData
70
     */
71 2
    public function processData_postProcessAction($action, array $cmdArr, array $result, ExtendedFileUtility $parentObject)
72
    {
73 2
        $file = $this->getFile($result);
74 2
        if ($this->isPurgingOfImgixCacheRequired($action, $file)) {
75 1
            $this->imagePurgeService->purgeImgixCache($this->buildImgUrl($file));
0 ignored issues
show
Bug introduced by
It seems like $file defined by $this->getFile($result) on line 73 can be null; however, Aoe\Imgix\TYPO3\FileUtil...DataHook::buildImgUrl() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
76 1
        }
77 2
    }
78
79
    /**
80
     * @param File $file
81
     * @return string
82
     */
83 2
    protected function buildImgUrl(File $file)
84
    {
85 2
        return sprintf('http://%s/%s', $this->configuration->getHost(), $file->getPublicUrl());
86
    }
87
88
    /**
89
     * @param array $result
90
     * @return File|null
91
     */
92 3
    protected function getFile(array $result)
93
    {
94 3
        if (isset($result[0]) && isset($result[0][0]) && $result[0][0] instanceof File) {
95 2
            return $result[0][0];
96
        }
97 1
        return null;
98
    }
99
100
    /**
101
     * We must purge the imgix-cache, when:
102
     *  - editor has updated an existing image (by uploading and overwriting an existing image)
103
     *  - editor has replaced an existing image
104
     *
105
     * @param string $action
106
     * @param File|null $file
107
     * @return boolean
108
     */
109 5
    protected function isPurgingOfImgixCacheRequired($action, File $file = null)
110
    {
111 5
        if ($file === null || $file->getType() !== File::FILETYPE_IMAGE) {
112 1
            return false;
113
        }
114 4
        if ($action === 'replace') {
115 1
            return true;
116
        }
117 3
        if ($action === 'upload' && count($file->getUpdatedProperties()) > 0) {
118
            // editor has updated an existing image (by uploading and overwriting an existing image)
119 2
            return true;
120
        }
121 1
        return false;
122
    }
123
}
124