ResolvesCurrentUser::getCurrentUserDataId()   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 seregazhuk\HeadHunterApi\EndPoints\Me;
6
use seregazhuk\HeadHunterApi\EndPoints\EndpointsContainer;
7
use seregazhuk\HeadHunterApi\Exceptions\HeadHunterApiException;
8
use seregazhuk\HeadHunterApi\Exceptions\WrongEndPointException;
9
10
/**
11
 * Trait ResolvesCurrentUser
12
 *
13
 * @property EndpointsContainer $container
14
 */
15
trait ResolvesCurrentUser
16
{
17
    /**
18
     * @return array
19
     * @throws WrongEndPointException
20
     */
21
    protected function getCurrentUserInfo()
22
    {
23
        /** @var Me $meEndpoint */
24
        $meEndpoint = $this->container->getEndpoint('me');
25
26
        return $meEndpoint->info();
27
    }
28
29
    /**
30
     * @param string $key
31
     * @return string
32
     * @throws HeadHunterApiException
33
     */
34
    protected function getCurrentUserDataId($key)
35
    {
36
        $currentUser = $this->getCurrentUserInfo();
37
38
        if (!isset($currentUser[$key]['id'])) {
39
            throw new HeadHunterApiException("Cannot resolve $key id");
40
        }
41
42
        return $currentUser[$key]['id'];
43
    }
44
45
    /**
46
     * @return string
47
     * @throws HeadHunterApiException
48
     */
49
    protected function getCurrentEmployerId()
50
    {
51
        return $this->getCurrentUserDataId('employer');
52
    }
53
54
    /**
55
     * @return string
56
     * @throws HeadHunterApiException
57
     */
58
    protected function getCurrentManagerId()
59
    {
60
        return $this->getCurrentUserDataId('manager');
61
    }
62
}
63