Passed
Push — master ( 0456bd...25822b )
by Mihail
07:10
created

ActionYandexCounter   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 30
rs 10
c 0
b 0
f 0
wmc 6

1 Method

Rating   Name   Duplication   Size   Complexity  
A yandexCounter() 0 22 6
1
<?php
2
3
namespace Apps\Controller\Admin\Main;
4
5
6
use Apps\Model\Admin\Main\FormYandexCounter;
7
use Ffcms\Core\App;
8
use Ffcms\Core\Arch\View;
9
use Ffcms\Core\Exception\ForbiddenException;
10
use Ffcms\Core\Helper\Type\Any;
11
use Ffcms\Core\Network\Request;
12
use Ffcms\Core\Network\Response;
13
use Ffcms\Yandex\Metrika\Client;
14
15
/**
16
 * Trait ActionYandexCounter
17
 * @package Apps\Controller\Admin\Main
18
 * @property Request $request
19
 * @property Response $response
20
 * @property View $view
21
 */
22
trait ActionYandexCounter
23
{
24
    /**
25
     * Select counter from api ids
26
     * @return string|null
27
     * @throws ForbiddenException
28
     * @throws \Ffcms\Core\Exception\SyntaxException
29
     */
30
    public function yandexCounter(): ?string
31
    {
32
        $configs = App::$Properties->get('oauth', 'Yandex');
33
34
        $client = new Client($configs['token'], 0);
35
        $counters = $client->getCountersList();
0 ignored issues
show
Bug introduced by zenn
Are you sure the assignment to $counters is correct as $client->getCountersList() targeting Ffcms\Yandex\Metrika\Client::getCountersList() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
36
37
        // check if counters exist
38
        if (!$counters || !Any::isArray($counters) || count($counters) < 1) {
0 ignored issues
show
introduced by zenn
$counters is defined implicitly as null, thus it is always evaluated to false.
Loading history...
Bug introduced by zenn
$counters of type void is incompatible with the type Countable|array expected by parameter $var of count(). ( Ignorable by Annotation )

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

38
        if (!$counters || !Any::isArray($counters) || count(/** @scrutinizer ignore-type */ $counters) < 1) {
Loading history...
39
            throw new ForbiddenException(__('No active counters found'));
40
        }
41
42
        // initialize select model
43
        $model = new FormYandexCounter($counters);
44
        if ($model->send() && $model->validate()) {
45
            $model->make();
46
            App::$Session->getFlashBag()->add('success', __('Yandex.metrika connected successful'));
47
            $this->response->redirect('main/index');
48
        }
49
50
        return $this->view->render('main/yandex_counter', [
51
            'model' => $model
52
        ]);
53
    }
54
}