Artists::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 11
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 13
rs 9.9
1
<?php declare(strict_types=1);
2
3
namespace XoopsModules\Songlist;
4
5
require_once \dirname(__DIR__) . '/include/songlist.object.php';
6
// require_once \dirname(__DIR__) . '/include/songlist.form.php';
7
use  XoopsModules\Songlist\Form\FormController;
8
9
/**
10
 * Class Artists
11
 */
12
class Artists extends \XoopsObject
13
{
14
    /**
15
     * Artists constructor.
16
     * @param null $fid
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $fid is correct as it would always require null to be passed?
Loading history...
17
     */
18
    public function __construct($fid = null)
19
    {
20
        $this->initVar('aid', \XOBJ_DTYPE_INT, 0, false);
21
        $this->initVar('cids', \XOBJ_DTYPE_ARRAY, [], false);
22
        $this->initVar('sids', \XOBJ_DTYPE_ARRAY, [], false);
23
        $this->initVar('name', \XOBJ_DTYPE_TXTBOX, null, false, 128);
24
        $this->initVar('albums', \XOBJ_DTYPE_INT, 0, false);
25
        $this->initVar('songs', \XOBJ_DTYPE_INT, 0, false);
26
        $this->initVar('hits', \XOBJ_DTYPE_INT, 0, false);
27
        $this->initVar('rank', \XOBJ_DTYPE_DECIMAL, 0, false);
28
        $this->initVar('votes', \XOBJ_DTYPE_INT, 0, false);
29
        $this->initVar('created', \XOBJ_DTYPE_INT, 0, false);
30
        $this->initVar('updated', \XOBJ_DTYPE_INT, 0, false);
31
    }
32
33
    /**
34
     * @param bool $as_array
35
     * @return array|string
36
     */
37
    public function getForm($as_array = false)
38
    {
39
        return FormController::getFormArtists($this, $as_array);
40
    }
41
42
    /**
43
     * @param bool $extra
44
     * @return array
45
     */
46
    public function toArray($extra = false): array
47
    {
48
        $ret  = parent::toArray();
49
        $form = $this->getForm(true);
50
        foreach ($form as $key => $element) {
51
            $ret['form'][$key] = $element->render();
52
        }
53
        foreach (['created', 'updated'] as $key) {
54
            if ($this->getVar($key) > 0) {
55
                $ret['form'][$key] = \date(_DATESTRING, $this->getVar($key));
56
                $ret[$key]         = \date(_DATESTRING, $this->getVar($key));
57
            }
58
        }
59
60
        $ret['rank'] = \number_format(($this->getVar('rank') > 0 && $this->getVar('votes') > 0 ? $this->getVar('rank') / $this->getVar('votes') : 0), 2) . \_MI_SONGLIST_OFTEN;
61
        $ret['url']  = $this->getURL(true);
0 ignored issues
show
Unused Code introduced by
The call to XoopsModules\Songlist\Artists::getURL() has too many arguments starting with true. ( Ignorable by Annotation )

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

61
        /** @scrutinizer ignore-call */ 
62
        $ret['url']  = $this->getURL(true);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
62
63
        \xoops_loadLanguage('enum', 'songlist');
64
        if (!empty($ret['singer'])) {
65
            $ret['singer'] = \constant($ret['singer']);
66
        }
67
68
        if (!$extra) {
69
            return $ret;
70
        }
71
72
        if (0 != \count($this->getVar('cids'))) {
0 ignored issues
show
Bug introduced by
It seems like $this->getVar('cids') can also be of type boolean and null and string; however, parameter $value of count() does only seem to accept Countable|array, maybe add an additional type check? ( Ignorable by Annotation )

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

72
        if (0 != \count(/** @scrutinizer ignore-type */ $this->getVar('cids'))) {
Loading history...
73
            $categoriesHandler = Helper::getInstance()->getHandler('Category');
74
            foreach ($this->getVar('cids') as $aid) {
75
                $category = $categoriesHandler->get($aid);
76
                if (\is_object($category)) {
77
                    $ret['categories_array'][$aid] = $category->toArray(false);
0 ignored issues
show
Unused Code introduced by
The call to XoopsObject::toArray() has too many arguments starting with false. ( Ignorable by Annotation )

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

77
                    /** @scrutinizer ignore-call */ 
78
                    $ret['categories_array'][$aid] = $category->toArray(false);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
78
                }
79
            }
80
        }
81
82
        if (\is_array($this->getVar('aids')) && 0 != \count($this->getVar('aids'))) {
83
            $artistsHandler = Helper::getInstance()->getHandler('Artists');
84
            foreach ($this->getVar('aids') as $aid) {
85
                $artist = $artistsHandler->get($aid);
86
                if (\is_object($artist)) {
87
                    $ret['artists_array'][$aid] = $artist->toArray(false);
88
                }
89
            }
90
        }
91
92
        if (0 != \count($this->getVar('sids'))) {
93
            $songsHandler = Helper::getInstance()->getHandler('Songs');
94
            $criteria     = new \Criteria('aids', '%"' . $this->getVar('aid') . '"%', 'LIKE');
95
            $criteria->setSort('songid');
96
            $criteria->setOrder('ASC');
97
            foreach ($songsHandler->getObjects($criteria, true) as $sid => $song) {
98
                if (\is_object($song)) {
99
                    $ret['songs_array'][$sid] = $song->toArray(false);
100
                }
101
            }
102
        }
103
104
        return $ret;
105
    }
106
107
    /**
108
     * @return string
109
     */
110
    public function getURL(): string
111
    {
112
        global $file, $op, $fct, $id, $value, $gid, $vid, $vcid, $cid, $start, $limit;
113
        if ($GLOBALS['songlistModuleConfig']['htaccess']) {
114
            if (0 != $id) {
115
                $artistHandler = Helper::getInstance()->getHandler('Artists');
116
                $artist        = $artistHandler->get($id);
117
                if (\is_object($artist) && !$artist->isNew()) {
118
                    return XOOPS_URL
119
                           . '/'
120
                           . $GLOBALS['songlistModuleConfig']['baseofurl']
121
                           . '/artists/'
122
                           . \urlencode(\str_replace([' ', \chr(9)], '-', $artist->getVar('name')))
123
                           . '/'
124
                           . $start
125
                           . '-'
126
                           . $id
127
                           . '-'
128
                           . $op
129
                           . '-'
130
                           . $fct
131
                           . '-'
132
                           . $gid
133
                           . '-'
134
                           . $cid
135
                           . '/'
136
                           . \urlencode($value)
137
                           . $GLOBALS['songlistModuleConfig']['endofurl'];
138
                }
139
140
                return XOOPS_URL . '/' . $GLOBALS['songlistModuleConfig']['baseofurl'] . '/artists/' . $start . '-' . $id . '-' . $op . '-' . $fct . '-' . $gid . '-' . $cid . '-' . $vcid . '/' . \urlencode($value) . $GLOBALS['songlistModuleConfig']['endofurl'];
141
            }
142
143
            return XOOPS_URL . '/' . $GLOBALS['songlistModuleConfig']['baseofurl'] . '/artists/' . $start . '-' . $id . '-' . $op . '-' . $fct . '-' . $gid . '-' . $cid . '-' . $vcid . '/' . \urlencode($value) . $GLOBALS['songlistModuleConfig']['endofurl'];
144
        }
145
146
        return XOOPS_URL . '/modules/songlist/artists.php?op=' . $op . '&fct=' . $fct . '&id=' . $id . '&value=' . \urlencode($value ?? '') . '&gid=' . $gid . '&vid=' . $vid . '&cid=' . $cid . '&start=' . $start;
147
    }
148
}
149