ReadmesHandler   A
last analyzed

Complexity

Total Complexity 32

Size/Duplication

Total Lines 281
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 116
dl 0
loc 281
rs 9.84
c 1
b 0
f 1
wmc 32

12 Methods

Rating   Name   Duplication   Size   Complexity  
A convertMD() 0 7 1
C updateTableReadmes() 0 50 12
A get() 0 3 1
B updateReadmes() 0 49 9
A getCountReadmes() 0 5 1
A updateRepoReadme() 0 10 1
A __construct() 0 3 1
A create() 0 3 1
A getReadmesCriteria() 0 7 1
A getInsertId() 0 3 1
A getAllReadmes() 0 5 1
A getFormFilterReadmes() 0 31 2
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
use XoopsModules\Wggithub\{
28
    Constants,
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, XoopsModules\Wggithub\Constants. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
29
    MDParser
30
};
31
32
33
/**
34
 * Class Object Handler Readmes
35
 */
36
class ReadmesHandler 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...
37
{
38
    /**
39
     * Constructor
40
     *
41
     * @param \XoopsDatabase $db
42
     */
43
    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...
44
    {
45
        parent::__construct($db, 'wggithub_readmes', Readmes::class, 'rm_id', 'rm_name');
46
    }
47
48
    /**
49
     * @param bool $isNew
50
     *
51
     * @return object
52
     */
53
    public function create($isNew = true)
54
    {
55
        return parent::create($isNew);
56
    }
57
58
    /**
59
     * retrieve a field
60
     *
61
     * @param int $i field id
62
     * @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...
63
     * @return mixed reference to the {@link Get} object
64
     */
65
    public function get($i = null, $fields = null)
66
    {
67
        return parent::get($i, $fields);
68
    }
69
70
    /**
71
     * get inserted id
72
     *
73
     * @param null
74
     * @return int reference to the {@link Get} object
75
     */
76
    public function getInsertId()
77
    {
78
        return $this->db->getInsertId();
79
    }
80
81
    /**
82
     * Get Count Readmes in the database
83
     * @param int    $start
84
     * @param int    $limit
85
     * @param string $sort
86
     * @param string $order
87
     * @return int
88
     */
89
    public function getCountReadmes($start = 0, $limit = 0, $sort = 'rm_id ASC, rm_name', $order = 'ASC')
90
    {
91
        $crCountReadmes = 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...
92
        $crCountReadmes = $this->getReadmesCriteria($crCountReadmes, $start, $limit, $sort, $order);
93
        return $this->getCount($crCountReadmes);
94
    }
95
96
    /**
97
     * Get All Readmes in the database
98
     * @param int    $start
99
     * @param int    $limit
100
     * @param string $sort
101
     * @param string $order
102
     * @return array
103
     */
104
    public function getAllReadmes($start = 0, $limit = 0, $sort = 'rm_id ASC, rm_name', $order = 'ASC')
105
    {
106
        $crAllReadmes = new \CriteriaCompo();
107
        $crAllReadmes = $this->getReadmesCriteria($crAllReadmes, $start, $limit, $sort, $order);
108
        return $this->getAll($crAllReadmes);
109
    }
110
111
    /**
112
     * Get Criteria Readmes
113
     * @param        $crReadmes
114
     * @param int    $start
115
     * @param int    $limit
116
     * @param string $sort
117
     * @param string $order
118
     * @return int
119
     */
120
    private function getReadmesCriteria($crReadmes, $start, $limit, $sort, $order)
121
    {
122
        $crReadmes->setStart($start);
123
        $crReadmes->setLimit($limit);
124
        $crReadmes->setSort($sort);
125
        $crReadmes->setOrder($order);
126
        return $crReadmes;
127
    }
128
129
    /**
130
     * Update table requests
131
     *
132
     * @return boolean
133
     */
134
    public function updateTableReadmes()
135
    {
136
        $helper = Wggithub\Helper::getInstance();
137
        $repositoriesHandler = $helper->getHandler('Repositories');
138
        $readmesHandler = $helper->getHandler('Readmes');
139
140
        $libRepositories = new Wggithub\Github\Repositories();
141
142
        $submitter = isset($GLOBALS['xoopsUser']) && \is_object($GLOBALS['xoopsUser']) ? $GLOBALS['xoopsUser']->getVar('uid') : 0;
143
144
        $repositoriesCount = $repositoriesHandler->getCount();
145
        if ($repositoriesCount > 0) {
146
            $repositoriesAll = $repositoriesHandler->getAll();
147
            foreach (\array_keys($repositoriesAll) as $i) {
148
                $repoId = $repositoriesAll[$i]->getVar('repo_id');
149
                $repoStatus = $repositoriesAll[$i]->getVar('repo_status');
150
                $crReadmes = new \CriteriaCompo();
151
                $crReadmes->add(new \Criteria('rm_repoid', $repoId));
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...
152
                $readmesCount = $readmesHandler->getCount($crReadmes);
153
                if (Constants::STATUS_NEW === $repoStatus || Constants::STATUS_UPDATED === $repoStatus || 0 == $readmesCount) {
154
                    $readme = $libRepositories->getReadme($repositoriesAll[$i]->getVar('repo_user'), $repositoriesAll[$i]->getVar('repo_name'));
155
                    if ($readmesCount > 0) {
156
                        $readmesAll = $readmesHandler->getAll($crReadmes);
157
                        foreach (\array_keys($readmesAll) as $j) {
158
                            $rmId = $readmesAll[$j]->getVar('rm_id');
159
                        }
160
                        unset($crReadmes, $readmesAll);
161
                        $readmesObj = $readmesHandler->get($rmId);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $rmId does not seem to be defined for all execution paths leading up to this point.
Loading history...
162
                    } else {
163
                        $readmesObj = $readmesHandler->create();
164
                    }
165
                }
166
                if (\is_array($readme)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $readme does not seem to be defined for all execution paths leading up to this point.
Loading history...
167
                    $readmesObj->setVar('rm_repoid', $repoId);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $readmesObj does not seem to be defined for all execution paths leading up to this point.
Loading history...
168
                    $readmesObj->setVar('rm_name', $readme['name']);
169
                    $readmesObj->setVar('rm_type', $readme['type']);
170
                    $readmesObj->setVar('rm_content', $readme['content']);
171
                    $readmesObj->setVar('rm_encoding', $readme['encoding']);
172
                    $readmesObj->setVar('rm_downloadurl', $readme['download_url']);
173
                    $readmesObj->setVar('rm_datecreated',\time());
174
                    $readmesObj->setVar('rm_submitter', $submitter);
175
                    // Insert Data
176
                    if (!$readmesHandler->insert($readmesObj)) {
177
                        return false;
178
                    }
179
                }
180
            }
181
        }
182
183
        return true;
184
    }
185
186
187
    /**
188
     * Update table requests
189
     *
190
     * @param $repoId
191
     * @param $userName
192
     * @param $repoName
193
     * @return boolean
194
     */
195
    public function updateReadmes($repoId, $userName, $repoName)
196
    {
197
        $helper = Wggithub\Helper::getInstance();
198
        $readmesHandler = $helper->getHandler('Readmes');
199
200
        $githubClient = new Wggithub\Github\GithubClient();
201
202
        $submitter = isset($GLOBALS['xoopsUser']) && \is_object($GLOBALS['xoopsUser']) ? $GLOBALS['xoopsUser']->getVar('uid') : 0;
203
204
        $readme = $githubClient->getReadme($userName, $repoName);
205
        if (false === $readme) {
206
            return false;
207
        }
208
        if (\count($readme) > 0) {
209
            if (\array_key_exists('message', $readme)) {
210
                // not readme found
211
                // must return true otherwise releases will not be loaded
212
                return true;
213
            }
214
            $rmId = 0;
215
            $crReadmes = new \CriteriaCompo();
216
            $crReadmes->add(new \Criteria('rm_repoid', $repoId));
217
            $readmesAll = $readmesHandler->getAll($crReadmes);
218
            foreach (\array_keys($readmesAll) as $j) {
219
                $rmId = $readmesAll[$j]->getVar('rm_id');
220
            }
221
            unset($crReadmes, $readmesAll);
222
            if ($rmId > 0) {
223
                $readmesObj = $readmesHandler->get($rmId);
224
            } else {
225
                $readmesObj = $readmesHandler->create();
226
            }
227
            $readmesObj->setVar('rm_repoid', $repoId);
228
            $readmesObj->setVar('rm_name', $readme['name']);
229
            $readmesObj->setVar('rm_type', $readme['type']);
230
            $readmesObj->setVar('rm_content', $readme['content']);
231
            $readmesObj->setVar('rm_encoding', $readme['encoding']);
232
            $readmesObj->setVar('rm_downloadurl', $readme['download_url']);
233
            $baseurl = \substr($readme['html_url'], 0, \strrpos($readme['html_url'], '/') + 1);
234
            $readmesObj->setVar('rm_baseurl', $baseurl);
235
            $readmesObj->setVar('rm_datecreated',\time());
236
            $readmesObj->setVar('rm_submitter', $submitter);
237
            // Insert Data
238
            if (!$readmesHandler->insert($readmesObj)) {
239
                return false;
240
            }
241
        }
242
243
        return true;
244
    }
245
246
    /**
247
     * convert md file content into clean text
248
     *
249
     * @param $contentDecoded
250
     * @return string
251
     */
252
    public function convertMD($contentDecoded)
253
    {
254
        // parse MD file
255
        $Parsedown = new MDParser\Parsedown();
256
        $contentClean = $Parsedown->text($contentDecoded);
257
258
        return $contentClean;
259
    }
260
261
    /**
262
     * Update table repositories with readme information
263
     *
264
     * @return boolean
265
     */
266
    public function updateRepoReadme()
267
    {
268
        // update repo_readme
269
        $sql = 'UPDATE ' . $GLOBALS['xoopsDB']->prefix('wggithub_repositories') . ' INNER JOIN ' . $GLOBALS['xoopsDB']->prefix('wggithub_readmes');
270
        $sql .= ' ON ' . $GLOBALS['xoopsDB']->prefix('wggithub_repositories') . '.repo_id = ' . $GLOBALS['xoopsDB']->prefix('wggithub_readmes') . '.rm_repoid ';
271
        $sql .= 'SET ' . $GLOBALS['xoopsDB']->prefix('wggithub_repositories') . '.repo_readme = 1 ';
272
        $sql .= 'WHERE (((' . $GLOBALS['xoopsDB']->prefix('wggithub_readmes') . '.rm_id)>0));';
273
        $GLOBALS['xoopsDB']->queryF($sql);
274
275
        return true;
276
    }
277
278
    /**
279
     * @public function getForm
280
     * @param bool   $action
281
     * @param int    $start
282
     * @param int    $limit
283
     * @param string $filterValue
284
     * @return \XoopsSimpleForm
285
     */
286
    public function getFormFilterReadmes($action = false, $start = 0, $limit = 0, $filterValue = '')
287
    {
288
        if (!$action) {
289
            $action = $_SERVER['REQUEST_URI'];
290
        }
291
        // Get Theme Form
292
        \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

292
        /** @scrutinizer ignore-call */ \xoops_load('XoopsFormLoader');
Loading history...
293
        $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...
294
        $form->setExtra('enctype="multipart/form-data"');
295
        $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...
296
        // Form Select field
297
        $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...
298
        $fieldSelect->addOption('', ' ');
299
        $fieldSelect->addOption('repo_name', \_AM_WGGITHUB_README_REPOID);
300
        $filterTray->addElement($fieldSelect, true);
301
        // Form Select operand
302
        $operandsSelect = new \XoopsFormSelect('', 'filter_operand', Constants::FILTER_OPERAND_LIKE);
303
        $operandsSelect->addOption(Constants::FILTER_OPERAND_EQUAL, \_AM_WGGITHUB_FILTER_OPERAND_EQUAL);
304
        $operandsSelect->addOption(Constants::FILTER_OPERAND_LIKE, \_AM_WGGITHUB_FILTER_OPERAND_LIKE);
305
        $filterTray->addElement($operandsSelect);
306
        // Form Text value
307
        $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...
308
        // Form button
309
        $filterTray->addElement(new \XoopsFormButton('', 'confirm_submit', \_SUBMIT, 'submit'));
0 ignored issues
show
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...
Bug introduced by
The constant _SUBMIT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
310
        $form->addElement($filterTray);
311
        // To Save
312
        $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...
313
        $form->addElement(new \XoopsFormHidden('start', $start));
314
        $form->addElement(new \XoopsFormHidden('limit', $limit));
315
316
        return $form;
317
    }
318
319
}
320