EmployerManagers::getManagersWhoHasVacancies()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
namespace seregazhuk\HeadHunterApi\Traits;
4
5
use UnexpectedValueException;
6
7
trait EmployerManagers
8
{
9
    use ResolvesCurrentUser;
10
11
    /**
12
     * Reference types and the rights of the Manager
13
     * @param string|bool $employerId
14
     * @return array
15
     */
16
    public function getManagerTypes($employerId = false)
17
    {
18
        $employerId = $this->resolveEmployerId($employerId);
19
20
        return $this->getResource("$employerId/manager_types", []);
0 ignored issues
show
Bug introduced by
It seems like getResource() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
21
    }
22
23
    /**
24
     * Get employer managers
25
     *
26
     * @param string|bool $employerId default resolved from profile
27
     * @return array
28
     */
29
    public function getManagers($employerId = false)
30
    {
31
        $employerId = $this->resolveEmployerId($employerId);
32
33
        return $this->getResource("$employerId/managers", []);
0 ignored issues
show
Bug introduced by
It seems like getResource() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
34
    }
35
36
    /**
37
     * Get manager information
38
     *
39
     * @param string $managerId
40
     * @param string|bool $employerId default resolved from profile
41
     * @return array
42
     */
43
    public function getManager($managerId, $employerId = false)
44
    {
45
        $employerId = $this->resolveEmployerId($employerId);
46
47
        return $this->getResource("$employerId/managers/$managerId", []);
0 ignored issues
show
Bug introduced by
It seems like getResource() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
48
    }
49
50
    /**
51
     * @param string|bool $employerId default resolved from profile
52
     * @return array
53
     */
54
    public function getManagersWhoHasVacancies($employerId = false)
55
    {
56
        $managers = $this->getManagers($employerId);
57
58
        if(!isset($managers['items']) ) {
59
            throw new UnexpectedValueException('Failed to get employer managers');
60
        }
61
62
        return array_filter($managers['items'], [$this, 'hasVacancies']);
63
    }
64
65
	/**
66
	 * @param array $manager
67
	 * @return bool
68
	 */
69
    protected function hasVacancies(array $manager)
70
    {
71
        return $manager['vacancies_count'] > 0;
72
    }
73
74
	/**
75
	 * @param string|bool $employerId
76
	 * @return string
77
	 */
78
    protected function resolveEmployerId($employerId)
79
    {
80
        return (false === $employerId)
81
            ? $this->getCurrentEmployerId()
82
            : $employerId
83
        ;
84
    }
85
}
86