Group::getCurrentPoint()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
namespace HDNET\Focuspoint\Service\WizardHandler;
4
5
use HDNET\Focuspoint\Domain\Repository\FileStandaloneRepository;
6
use TYPO3\CMS\Core\Utility\GeneralUtility;
7
8
/**
9
 * Group.
10
 */
11
class Group extends AbstractWizardHandler
12
{
13
    /**
14
     * Check if the handler can handle the current request.
15
     *
16
     * @return bool
17
     */
18
    public function canHandle()
19
    {
20
        return null !== $this->getRelativeFilePath();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return null !== $this->getRelativeFilePath(); (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...
21
    }
22
23
    /**
24
     * Return the current point.
25
     *
26
     * @return int[]
27
     */
28
    public function getCurrentPoint()
29
    {
30
        $fileStandaloneRepository = GeneralUtility::makeInstance(FileStandaloneRepository::class);
31
        $row = $fileStandaloneRepository->findOneByRelativeFilePath($this->getRelativeFilePath());
32
        if (!isset($row['focus_point_x'])) {
33
            return [0, 0];
34
        }
35
36
        return $this->cleanupPosition([
37
            $row['focus_point_x'],
38
            $row['focus_point_y'],
39
        ]);
40
    }
41
42
    /**
43
     * Set the point (between -100 and 100).
44
     *
45
     * @param int $x
46
     * @param int $y
47
     */
48
    public function setCurrentPoint($x, $y)
49
    {
50
        $fileStandaloneRepository = GeneralUtility::makeInstance(FileStandaloneRepository::class);
51
        $row = $fileStandaloneRepository->findOneByRelativeFilePath($this->getRelativeFilePath());
52
53
        $values = [
54
            'focus_point_x' => $x,
55
            'focus_point_y' => $y,
56
            'relative_file_path' => $this->getRelativeFilePath(),
57
        ];
58
59
        if (isset($row['uid'])) {
60
            $fileStandaloneRepository->update(
61
                (int) $row['uid'],
62
                $values
63
            );
64
        } else {
65
            $fileStandaloneRepository->insert(
66
                $values
67
            );
68
        }
69
    }
70
71
    /**
72
     * Get the public URL for the current handler.
73
     *
74
     * @return string
75
     */
76
    public function getPublicUrl()
77
    {
78
        return $this->displayableImageUrl($this->getRelativeFilePath());
79
    }
80
81
    /**
82
     * get the arguments for same request call.
83
     *
84
     * @return array
85
     */
86
    public function getArguments()
87
    {
88
        $parameter = GeneralUtility::_GET();
89
        $p = $parameter['P'];
90
91
        return [
92
            'P' => [
93
                'table' => $p['table'],
94
                'field' => $p['field'],
95
                'file' => $p['file'],
96
            ],
97
        ];
98
    }
99
100
    /**
101
     * get the file name.
102
     *
103
     * @return string|null
104
     */
105
    protected function getRelativeFilePath()
106
    {
107
        $parameter = GeneralUtility::_GET();
108
        if (!isset($parameter['P'])) {
109
            return;
110
        }
111
        $p = $parameter['P'];
112
        if (!isset($p['table']) || !isset($p['field']) || !isset($p['file'])) {
113
            return;
114
        }
115
        if (!isset($GLOBALS['TCA'][$p['table']])) {
116
            return;
117
        }
118
        $tableTca = $GLOBALS['TCA'][$p['table']];
119
        if (!isset($tableTca['columns'][$p['field']])) {
120
            return;
121
        }
122
        $fieldTca = $tableTca['columns'][$p['field']];
123
124
        $uploadFolder = $fieldTca['config']['uploadfolder'] ?? '';
125
        $baseFolder = '';
126
        if ('' !== \trim($uploadFolder, '/')) {
127
            $baseFolder = \rtrim($uploadFolder, '/') . '/';
128
        }
129
130
        $filePath = $baseFolder . $p['file'];
131
        if (!\is_file(GeneralUtility::getFileAbsFileName($filePath))) {
132
            return;
133
        }
134
135
        return $filePath;
136
    }
137
}
138