Completed
Push — master ( 3149ed...9d4732 )
by Sergey
13s
created

EmployerManagers   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 79
Duplicated Lines 30.38 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 8
c 1
b 0
f 1
lcom 1
cbo 1
dl 24
loc 79
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getManagerTypes() 8 8 1
A getManagers() 8 8 1
A getManager() 8 8 1
A getManagersWhoHasVacancies() 0 10 2
A hasVacancies() 0 4 1
A resolveEmployerId() 0 7 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace seregazhuk\HeadHunterApi\Traits;
4
5
trait EmployerManagers
6
{
7
    use ResolvesCurrentUser;
8
9
    /**
10
     * Reference types and the rights of the Manager
11
     * @param  mixed $employerId
12
     * @return array
13
     */
14 View Code Duplication
    public function getManagerTypes($employerId = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
15
    {
16
        $employerId = $this->resolveEmployerId($employerId);
17
18
        $verb = sprintf("%s/manager_types", $employerId );
19
20
        return $this->getResource($verb, []);
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
    /**
25
     * Get employer managers
26
     *
27
     * @param  mixed $employerId default resolved from profile
28
     * @return array
29
     */
30 View Code Duplication
    public function getManagers($employerId = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
31
    {
32
        $employerId = $this->resolveEmployerId($employerId);
33
34
        $verb = sprintf("%s/managers", $employerId );
35
36
        return $this->getResource($verb, []);
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...
37
    }
38
39
40
    /**
41
     * Get manager information
42
     *
43
     * @param string $managerId
44
     * @param mixed $employerId default resolved from profile
45
     * @return array
46
     */
47 View Code Duplication
    public function getManager($managerId, $employerId = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

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