Completed
Push — master ( e44c51...6a3a9b )
by Tim
15:54
created

Group::getRelativeFilePath()   D

Complexity

Conditions 10
Paths 12

Size

Total Lines 31
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 31
rs 4.8196
cc 10
eloc 21
nc 12
nop 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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