Passed
Push — master ( 0deb8f...f54565 )
by Goffy
04:13
created

RepositoriesHandler::updateTableRepositories()   F

Complexity

Conditions 17
Paths 764

Size

Total Lines 79
Code Lines 57

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 17
eloc 57
nc 764
nop 3
dl 0
loc 79
rs 1.3777
c 0
b 0
f 0

How to fix   Long Method    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
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
     * @return int
132
     */
133
    public function updateTableRepositories($user, $repos = [], $updateAddtionals = true)
134
    {
135
        $reposNb = 0;
136
        $helper = \XoopsModules\Wggithub\Helper::getInstance();
137
        $repositoriesHandler = $helper->getHandler('Repositories');
138
139
        $submitter = isset($GLOBALS['xoopsUser']) && \is_object($GLOBALS['xoopsUser']) ? $GLOBALS['xoopsUser']->getVar('uid') : 0;
140
        // add/update all items from table repositories
141
        foreach ($repos as $key => $repo) {
142
            $crRepositories = new \CriteriaCompo();
143
            $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...
144
            $repoId = 0;
145
            $status = 0;
146
            $updatedAtOld = 0;
147
            $updatedAtNew = 0;
148
            $repositoriesAll = $repositoriesHandler->getAll($crRepositories);
149
            foreach (\array_keys($repositoriesAll) as $i) {
150
                $repoId = $repositoriesAll[$i]->getVar('repo_id');
151
                $updatedAtOld = $repositoriesAll[$i]->getVar('repo_updatedat');
152
                $status = $repositoriesAll[$i]->getVar('repo_status');
153
                $repositoriesObj = $repositoriesAll[$i];
154
            }
155
            if ($repoId > 0) {
156
                if (is_string($repo['updated_at'])) {
157
                    $updatedAtNew = \strtotime($repo['updated_at']);
158
                }
159
                if ($updatedAtOld != $updatedAtNew) {
160
                    $status = Constants::STATUS_UPDATED;
161
                }
162
            } else {
163
                $repositoriesObj = $repositoriesHandler->create();
164
                $updatedAtNew = \strtotime($repo['updated_at']);
165
                $status = Constants::STATUS_NEW;
166
            }
167
            if (Constants::STATUS_UPTODATE !== $status) {
168
                // Set Vars
169
                $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...
170
                $repositoriesObj->setVar('repo_user', $user);
171
                $repositoriesObj->setVar('repo_name', $repo['name']);
172
                $repositoriesObj->setVar('repo_fullname', $repo['full_name']);
173
                if (is_string($repo['created_at'])) {
174
                    $createdAt = \strtotime($repo['created_at']);
175
                }
176
                $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...
177
                $repositoriesObj->setVar('repo_updatedat', $updatedAtNew);
178
                $repositoriesObj->setVar('repo_htmlurl', $repo['html_url']);
179
                $repositoriesObj->setVar('repo_status', $status);
180
                $repositoriesObj->setVar('repo_datecreated', time());
181
                $repositoriesObj->setVar('repo_submitter', $submitter);
182
                // Insert Data
183
                if ($repositoriesHandler->insert($repositoriesObj)) {
184
                    $newRepoId = $repositoriesObj->getNewInsertedIdRepositories();
185
                    if (0 == $repoId) {
186
                        $repoId = $newRepoId;
187
                    }
188
                    $reposNb++;
189
                    if ($updateAddtionals && (Constants::STATUS_NEW == $status || Constants::STATUS_UPDATED == $status)) {
190
                        // add/update table readmes
191
                        $res = $helper->getHandler('Readmes')->updateReadmes($repoId, $user, $repo['name']);
192
                        if (false === $res) {
193
                            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...
194
                        }
195
                        // add/update table release
196
                        $res = $helper->getHandler('Releases')->updateReleases($repoId, $user, $repo['name']);
197
                        if (false === $res) {
198
                            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...
199
                        }
200
                        // change status to updated
201
                        $repositoriesObj = $repositoriesHandler->get($repoId);
202
                        $repositoriesObj->setVar('repo_status', Constants::STATUS_UPTODATE);
203
                        $repositoriesHandler->insert($repositoriesObj, true);
204
                    }
205
                } else {
206
                    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...
207
                }
208
            }
209
        }
210
211
        return $reposNb;
212
    }
213
214
215
}
216