File::getPublicUrl()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace HDNET\Focuspoint\Service\WizardHandler;
4
5
use HDNET\Focuspoint\Domain\Repository\SysFileMetadataRepository;
6
use HDNET\Focuspoint\Utility\FileUtility;
7
use TYPO3\CMS\Core\Utility\GeneralUtility;
8
use TYPO3\CMS\Core\Utility\MathUtility;
9
10
/**
11
 * File.
12
 */
13
class File extends AbstractWizardHandler
14
{
15
    /**
16
     * Check if the handler can handle the current request.
17
     *
18
     * @return bool
19
     */
20
    public function canHandle()
21
    {
22
        $uid = $this->getMataDataUid();
23
24
        return null !== $uid;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return null !== $uid; (boolean) is incompatible with the return type declared by the abstract method HDNET\Focuspoint\Service...izardHandler::canHandle of type HDNET\Focuspoint\Service\WizardHandler\true.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
25
    }
26
27
    /**
28
     * get the arguments for same request call.
29
     *
30
     * @return array
31
     */
32
    public function getArguments()
33
    {
34
        return [
35
            'P' => [
36
                'metaUid' => $this->getMataDataUid(),
37
            ],
38
        ];
39
    }
40
41
    /**
42
     * Return the current point.
43
     *
44
     * @return int[]
45
     */
46
    public function getCurrentPoint()
47
    {
48
        $row = GeneralUtility::makeInstance(SysFileMetadataRepository::class)->findByUid((int) $this->getMataDataUid());
49
50
        return $this->cleanupPosition([
51
            $row['focus_point_x'],
52
            $row['focus_point_y'],
53
        ]);
54
    }
55
56
    /**
57
     * Set the point (between -100 and 100).
58
     *
59
     * @param int $x
60
     * @param int $y
61
     */
62
    public function setCurrentPoint($x, $y)
63
    {
64
        $values = [
65
            'focus_point_x' => MathUtility::forceIntegerInRange($x, -100, 100, 0),
66
            'focus_point_y' => MathUtility::forceIntegerInRange($y, -100, 100, 0),
67
        ];
68
69
        GeneralUtility::makeInstance(SysFileMetadataRepository::class)->update((int) $this->getMataDataUid(), $values);
70
    }
71
72
    /**
73
     * Get the public URL for the current handler.
74
     *
75
     * @return string
76
     */
77
    public function getPublicUrl()
78
    {
79
        $fileObject = FileUtility::getFileByMetaData($this->getMataDataUid());
80
81
        return $this->displayableImageUrl($fileObject->getPublicUrl());
82
    }
83
84
    /**
85
     * Fetch the meta data UID.
86
     *
87
     * @return int|null
88
     */
89
    protected function getMataDataUid()
90
    {
91
        $parameter = GeneralUtility::_GET();
92
        if (!isset($parameter['P'])) {
93
            return;
94
        }
95
        $p = $parameter['P'];
96
        if (isset($p['metaUid']) && MathUtility::canBeInterpretedAsInteger($p['metaUid'])) {
97
            return (int) $p['metaUid'];
98
        }
99
        if (isset($p['table']) && 'sys_file_metadata' === $p['table'] && isset($p['uid']) && MathUtility::canBeInterpretedAsInteger($p['uid'])) {
100
            return (int) $p['uid'];
101
        }
102
    }
103
}
104