Completed
Push — feature/remove-old-raa-switche... ( e62d75 )
by Michiel
12:23
created

RaListingService::searchBy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
/**
4
 * Copyright 2018 SURFnet B.V.
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
namespace Surfnet\StepupRa\RaBundle\Service;
20
21
use Psr\Log\LoggerInterface;
22
use Surfnet\StepupMiddlewareClient\Identity\Dto\RaListingSearchQuery;
23
use Surfnet\StepupMiddlewareClientBundle\Identity\Dto\RaListingCollection;
24
use Surfnet\StepupMiddlewareClientBundle\Identity\Service\RaListingService as MiddlewareClientBundleRaListingService;
25
use Symfony\Component\Form\ChoiceList\ArrayChoiceList;
26
27
class RaListingService
28
{
29
    /**
30
     * @var MiddlewareClientBundleRaListingService
31
     */
32
    private $raListingService;
33
34
    /**
35
     * @var LoggerInterface
36
     */
37
    private $logger;
38
39
    public function __construct(
40
        MiddlewareClientBundleRaListingService $raListingService,
41
        LoggerInterface $logger
42
    ) {
43
        $this->raListingService = $raListingService;
44
        $this->logger = $logger;
45
    }
46
47
    /**
48
     * @param $institution
49
     * @return ArrayChoiceList
50
     */
51
    public function createChoiceListFor($institution)
52
    {
53
        $collection = $this->searchBy($institution);
54
55
        if ($collection->getTotalItems() === 0) {
56
            $this->logger->warning('No RAA institutions found for identity, unable to build the choice list for the RAA switcher');
57
            return new ArrayChoiceList();
0 ignored issues
show
Bug introduced by
The call to ArrayChoiceList::__construct() misses a required argument $choices.

This check looks for function calls that miss required arguments.

Loading history...
58
        }
59
60
        $choices = [];
61
        foreach ($collection as $item) {
62
            $choices[$item->institution] = $item->institution;
63
        }
64
65
        return new ArrayChoiceList($choices);
0 ignored issues
show
Documentation introduced by
$choices is of type array, but the function expects a object<Symfony\Component...rm\ChoiceList\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
66
    }
67
68
    /**
69
     * @param string $institution
70
     * @return RaListingCollection
71
     */
72
    public function searchBy($institution)
73
    {
74
        $query = new RaListingSearchQuery($institution, 1);
75
        $query->setIdentityId($institution);
76
        return $this->raListingService->search($query);
77
    }
78
}
79