RepositoriesHandler::getRepositoriesCriteria()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 5
c 1
b 0
f 1
nc 1
nop 5
dl 0
loc 7
rs 10
1
<?php
2
3
namespace XoopsModules\Wggithub;
4
5
/*
6
 You may not change or alter any portion of this comment or credits
7
 of supporting developers from this source code or any supporting source code
8
 which is considered copyrighted (c) material of the original comment or credit authors.
9
10
 This program is distributed in the hope that it will be useful,
11
 but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
*/
14
15
/**
16
 * wgGitHub module for xoops
17
 *
18
 * @copyright      2020 XOOPS Project (https://xooops.org)
19
 * @license        GPL 2.0 or later
20
 * @package        wggithub
21
 * @since          1.0
22
 * @min_xoops      2.5.10
23
 * @author         Goffy - XOOPS Development Team - Email:<[email protected]> - Website:<https://wedega.com>
24
 */
25
26
use XoopsModules\Wggithub;
27
28
29
/**
30
 * Class Object Handler Repositories
31
 */
32
class RepositoriesHandler extends \XoopsPersistableObjectHandler
0 ignored issues
show
Bug introduced by
The type XoopsPersistableObjectHandler was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
33
{
34
    /**
35
     * Constructor
36
     *
37
     * @param \XoopsDatabase $db
38
     */
39
    public function __construct(\XoopsDatabase $db)
0 ignored issues
show
Bug introduced by
The type XoopsDatabase was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
40
    {
41
        parent::__construct($db, 'wggithub_repositories', Repositories::class, 'repo_id', 'repo_name');
42
    }
43
44
    /**
45
     * @param bool $isNew
46
     *
47
     * @return object
48
     */
49
    public function create($isNew = true)
50
    {
51
        return parent::create($isNew);
52
    }
53
54
    /**
55
     * retrieve a field
56
     *
57
     * @param int $i field id
58
     * @param null fields
0 ignored issues
show
Bug introduced by
The type XoopsModules\Wggithub\fields was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
59
     * @return mixed reference to the {@link Get} object
60
     */
61
    public function get($i = null, $fields = null)
62
    {
63
        return parent::get($i, $fields);
64
    }
65
66
    /**
67
     * get inserted id
68
     *
69
     * @param null
70
     * @return int reference to the {@link Get} object
71
     */
72
    public function getInsertId()
73
    {
74
        return $this->db->getInsertId();
75
    }
76
77
    /**
78
     * Get Count Repositories in the database
79
     * @param int    $start
80
     * @param int    $limit
81
     * @param string $sort
82
     * @param string $order
83
     * @return int
84
     */
85
    public function getCountRepositories($start = 0, $limit = 0, $sort = 'repo_id ASC, repo_name', $order = 'ASC')
86
    {
87
        $crCountRepositories = new \CriteriaCompo();
0 ignored issues
show
Bug introduced by
The type CriteriaCompo was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
88
        $crCountRepositories = $this->getRepositoriesCriteria($crCountRepositories, $start, $limit, $sort, $order);
89
        return $this->getCount($crCountRepositories);
90
    }
91
92
    /**
93
     * Get All Repositories in the database
94
     * @param int    $start
95
     * @param int    $limit
96
     * @param string $sort
97
     * @param string $order
98
     * @return array
99
     */
100
    public function getAllRepositories($start = 0, $limit = 0, $sort = 'repo_id ASC, repo_name', $order = 'ASC')
101
    {
102
        $crAllRepositories = new \CriteriaCompo();
103
        $crAllRepositories = $this->getRepositoriesCriteria($crAllRepositories, $start, $limit, $sort, $order);
104
        return $this->getAll($crAllRepositories);
105
    }
106
107
    /**
108
     * Get Criteria Repositories
109
     * @param        $crRepositories
110
     * @param int    $start
111
     * @param int    $limit
112
     * @param string $sort
113
     * @param string $order
114
     * @return int
115
     */
116
    private function getRepositoriesCriteria($crRepositories, $start, $limit, $sort, $order)
117
    {
118
        $crRepositories->setStart($start);
119
        $crRepositories->setLimit($limit);
120
        $crRepositories->setSort($sort);
121
        $crRepositories->setOrder($order);
122
        return $crRepositories;
123
    }
124
125
    /**
126
     * Update table repositories
127
     *
128
     * @param       $user
129
     * @param array $repos
130
     * @param bool  $updateAddtionals
131
     * @param int   $dirContent
132
     * @return int
133
     */
134
    public function updateTableRepositories($user, $repos = [], $updateAddtionals = true, $dirContent = 0)
135
    {
136
        $reposNb = 0;
137
        $helper = \XoopsModules\Wggithub\Helper::getInstance();
138
        $repositoriesHandler = $helper->getHandler('Repositories');
139
140
        $submitter = isset($GLOBALS['xoopsUser']) && \is_object($GLOBALS['xoopsUser']) ? $GLOBALS['xoopsUser']->getVar('uid') : 0;
141
        // add/update all items from table repositories
142
        foreach ($repos as $key => $repo) {
143
            $fork = (bool)$repo['fork'];
144
            if (Constants::DIRECTORY_CONTENT_ALL == $dirContent || false === $fork) {
145
                $crRepositories = new \CriteriaCompo();
146
                $crRepositories->add(new \Criteria('repo_nodeid', $repo['node_id']));
0 ignored issues
show
Bug introduced by
The type Criteria was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
147
                $repoId = 0;
148
                $status = 0;
149
                $updatedAtOld = 0;
150
                $updatedAtNew = 0;
151
                $repositoriesAll = $repositoriesHandler->getAll($crRepositories);
152
                foreach (\array_keys($repositoriesAll) as $i) {
153
                    $repoId = $repositoriesAll[$i]->getVar('repo_id');
154
                    $updatedAtOld = $repositoriesAll[$i]->getVar('repo_updatedat');
155
                    $status = $repositoriesAll[$i]->getVar('repo_status');
156
                    $repositoriesObj = $repositoriesAll[$i];
157
                }
158
                if ($repoId > 0) {
159
                    if (\is_string($repo['pushed_at'])) {
160
                        $updatedAtNew = \strtotime($repo['pushed_at']);
161
                    } elseif (\is_string($repo['updated_at'])) {
162
                        $updatedAtNew = \strtotime($repo['updated_at']);
163
                    }
164
                    if ($updatedAtOld != Constants::STATUS_OFFLINE && $updatedAtOld != $updatedAtNew) {
165
                        $status = Constants::STATUS_UPDATED;
166
                    }
167
                } else {
168
                    $repositoriesObj = $repositoriesHandler->create();
169
                    $updatedAtNew = \strtotime($repo['updated_at']);
170
                    $status = Constants::STATUS_NEW;
171
                }
172
                if (Constants::STATUS_UPTODATE !== $status) {
173
                    // Set Vars
174
                    $repositoriesObj->setVar('repo_nodeid', $repo['node_id']);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $repositoriesObj does not seem to be defined for all execution paths leading up to this point.
Loading history...
175
                    $repositoriesObj->setVar('repo_user', $user);
176
                    $repositoriesObj->setVar('repo_name', $repo['name']);
177
                    $repositoriesObj->setVar('repo_fullname', $repo['full_name']);
178
                    if (\is_string($repo['created_at'])) {
179
                        $createdAt = \strtotime($repo['created_at']);
180
                    }
181
                    $repositoriesObj->setVar('repo_createdat', $createdAt);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $createdAt does not seem to be defined for all execution paths leading up to this point.
Loading history...
182
                    $repositoriesObj->setVar('repo_updatedat', $updatedAtNew);
183
                    $repositoriesObj->setVar('repo_htmlurl', $repo['html_url']);
184
                    $repositoriesObj->setVar('repo_status', $status);
185
                    $repositoriesObj->setVar('repo_datecreated', \time());
186
                    $repositoriesObj->setVar('repo_submitter', $submitter);
187
                    // Insert Data
188
                    if ($repositoriesHandler->insert($repositoriesObj)) {
189
                        $newRepoId = $repositoriesObj->getNewInsertedIdRepositories();
190
                        if (0 == $repoId) {
191
                            $repoId = $newRepoId;
192
                        }
193
                        $reposNb++;
194
                        if ($updateAddtionals && (Constants::STATUS_NEW == $status || Constants::STATUS_UPDATED == $status)) {
195
                            // add/update table readmes
196
                            $helper->getHandler('Readmes')->updateReadmes($repoId, $user, $repo['name']);
197
                            // add/update table release
198
                            $helper->getHandler('Releases')->updateReleases($repoId, $user, $repo['name']);
199
                            // change status to updated
200
                            $repositoriesObj = $repositoriesHandler->get($repoId);
201
                            $repositoriesObj->setVar('repo_status', Constants::STATUS_UPTODATE);
202
                            $repositoriesHandler->insert($repositoriesObj, true);
203
                        }
204
                    } else {
205
                        return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type integer.
Loading history...
206
                    }
207
                }
208
            }
209
        }
210
211
        return $reposNb;
212
    }
213
214
    /**
215
     * @public function getForm
216
     * @param bool   $action
217
     * @param int    $start
218
     * @param int    $limit
219
     * @param string $filterValue
220
     * @param int    $filterStatus
221
     * @return \XoopsSimpleForm
222
     */
223
    public function getFormFilterRepos($action = false, $start = 0, $limit = 0, $filterValue = '', $filterStatus = 0)
224
    {
225
        if (!$action) {
226
            $action = $_SERVER['REQUEST_URI'];
227
        }
228
        // Get Theme Form
229
        \xoops_load('XoopsFormLoader');
0 ignored issues
show
Bug introduced by
The function xoops_load was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

229
        /** @scrutinizer ignore-call */ \xoops_load('XoopsFormLoader');
Loading history...
230
        $form = new \XoopsSimpleForm('', 'formFilterAdmin', $action, 'post', true);
0 ignored issues
show
Bug introduced by
The type XoopsSimpleForm was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
231
        $form->setExtra('enctype="multipart/form-data"');
232
        $filterTray = new \XoopsFormElementTray('', '&nbsp;');
0 ignored issues
show
Bug introduced by
The type XoopsFormElementTray was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
233
        // Form Select field
234
        $fieldSelect = new \XoopsFormSelect(\_AM_WGGITHUB_FILTER, 'filter_field', 'repo_name');
0 ignored issues
show
Bug introduced by
The type XoopsFormSelect was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
235
        $fieldSelect->addOption('', ' ');
236
        $fieldSelect->addOption('repo_user', \_AM_WGGITHUB_REPOSITORY_USER);
237
        $fieldSelect->addOption('repo_name', \_AM_WGGITHUB_REPOSITORY_NAME);
238
        $fieldSelect->addOption('repo_fullname', \_AM_WGGITHUB_REPOSITORY_FULLNAME);
239
        $filterTray->addElement($fieldSelect, true);
240
        // Form Select operand
241
        $operandsSelect = new \XoopsFormSelect('', 'filter_operand', Constants::FILTER_OPERAND_LIKE);
242
        $operandsSelect->addOption(Constants::FILTER_OPERAND_EQUAL, \_AM_WGGITHUB_FILTER_OPERAND_EQUAL);
243
        $operandsSelect->addOption(Constants::FILTER_OPERAND_LIKE, \_AM_WGGITHUB_FILTER_OPERAND_LIKE);
244
        $filterTray->addElement($operandsSelect);
245
        // Form Text value
246
        $filterTray->addElement(new \XoopsFormText('', 'filter_value', 20, 255, $filterValue), true);
0 ignored issues
show
Bug introduced by
The type XoopsFormText was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
247
        // Form Select Status repoStatus
248
        $repoStatusSelect = new \XoopsFormSelect(\_AM_WGGITHUB_REPOSITORY_STATUS, 'filter_status', $filterStatus);
249
        $repoStatusSelect->addOption(Constants::STATUS_NONE, ' ');
250
        $repoStatusSelect->addOption(Constants::STATUS_UPTODATE, \_AM_WGGITHUB_STATUS_UPTODATE);
251
        $repoStatusSelect->addOption(Constants::STATUS_UPDATED, \_AM_WGGITHUB_STATUS_UPDATED);
252
        $repoStatusSelect->addOption(Constants::STATUS_OFFLINE, \_AM_WGGITHUB_STATUS_OFFLINE);
253
        $filterTray->addElement($repoStatusSelect);
254
        // Form button
255
        $filterTray->addElement(new \XoopsFormButton('', 'confirm_submit', \_SUBMIT, 'submit'));
0 ignored issues
show
Bug introduced by
The constant _SUBMIT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Bug introduced by
The type XoopsFormButton was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
256
        $form->addElement($filterTray);
257
        // To Save
258
        $form->addElement(new \XoopsFormHidden('op', 'filter'));
0 ignored issues
show
Bug introduced by
The type XoopsFormHidden was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
259
        $form->addElement(new \XoopsFormHidden('start', $start));
260
        $form->addElement(new \XoopsFormHidden('limit', $limit));
261
262
        return $form;
263
    }
264
}
265