Completed
Push — master ( a12959...164d8d )
by Mario
03:20
created

TogglFactory::buildToggable()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 64
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 45
CRAP Score 6

Importance

Changes 10
Bugs 0 Features 3
Metric Value
c 10
b 0
f 3
dl 0
loc 64
ccs 45
cts 45
cp 1
rs 8.6346
cc 6
eloc 42
nc 6
nop 1
crap 6

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Marek\Toggable\Factory;
4
5
use Marek\Toggable\Http\Client\NativeHttpClient;
6
use Marek\Toggable\Http\Converter\NativeArgumentsConverter;
7
use Marek\Toggable\Http\Manager\NativeRequestManager;
8
use Marek\Toggable\Http\Parser\NativeResponseParser;
9
use Marek\Toggable\Http\Parser\ResponseParser;
10
use Marek\Toggable\Hydrator\HydratorFactory;
11
use Marek\Toggable\Service\Authentication\AuthenticationService;
12
use Marek\Toggable\Service\Dashboard\DashboardService;
13
use Marek\Toggable\Service\Project\ProjectService;
14
use Marek\Toggable\Service\ProjectUsers\ProjectUsersService;
15
use Marek\Toggable\Service\Tag\TagService;
16
use Marek\Toggable\Service\Task\TaskService;
17
use Marek\Toggable\Service\TimeEntry\TimeEntryService;
18
use Marek\Toggable\Service\User\UserService;
19
use Marek\Toggable\Service\WorkspaceUsers\WorkspaceUsersService;
20
use Marek\Toggable\Toggl;
21
use Marek\Toggable\API\Security\UsernameAndPasswordToken;
22
use Marek\Toggable\API\Security\ApiToken;
23
use Marek\Toggable\Service\Client\ClientService;
24
use Marek\Toggable\Service\Workspace\WorkspaceService;
25
26
/**
27
 * Class TogglFactory
28
 * @package Marek\Toggable\Factory
29
 */
30
class TogglFactory
31
{
32
    /**
33
     * Builds Toggl
34
     *
35
     * @param array $config
36
     *
37
     * @return \Marek\Toggable\Toggl
38
     *
39
     * @throws \InvalidArgumentException
40
     */
41 17
    public static function buildToggable($config)
42 1
    {
43 17
        if (!is_array($config)) {
44 1
            throw new \InvalidArgumentException('Please provide valid configuration.');
45
        }
46
        
47 16
        if (!empty($config['marek_toggable']['security']['token'])) {
48
49 14
            $authentication = new ApiToken($config['marek_toggable']['security']['token']);
50
51 16
        } else if (!empty($config['marek_toggable']['security']['username']) && !empty($config['marek_toggable']['security']['password'])) {
52
53 1
            $authentication = new UsernameAndPasswordToken(
54 1
                $config['marek_toggable']['security']['username'],
55 1
                $config['marek_toggable']['security']['password']
56 1
            );
57
58 1
        } else {
59
60 1
            throw new \InvalidArgumentException('Please provide security configuration.');
61
62
        }
63
64 15
        if (empty($config['marek_toggable']['base_uri']))
65 15
        {
66 1
            throw new \InvalidArgumentException('Please provide base URI.');
67
        }
68
69 14
        $argumentConverter = new NativeArgumentsConverter();
70 14
        $responseParser = new NativeResponseParser();
71 14
        $nativeHttpClient = new NativeHttpClient($config['marek_toggable']['base_uri'], $authentication, $argumentConverter, $responseParser);
72 14
        $requestManager = new NativeRequestManager($nativeHttpClient);
73
74 14
        $hydrator = HydratorFactory::createHydrator();
75
76 14
        $authenticationService = new AuthenticationService($requestManager, $hydrator);
77 14
        $clientService = new ClientService($requestManager, $hydrator);
78 14
        $dashboardService = new DashboardService($requestManager, $hydrator);
79 14
        $projectService = new ProjectService($requestManager, $hydrator);
80 14
        $projectUsersService = new ProjectUsersService($requestManager, $hydrator);
81 14
        $tagService = new TagService($requestManager, $hydrator);
82 14
        $taskService = new TaskService($requestManager, $hydrator);
83 14
        $timeEntryService = new TimeEntryService($requestManager, $hydrator);
84
85 14
        $userService = new UserService($requestManager, $hydrator);
86 14
        $workspaceService = new WorkspaceService($requestManager, $hydrator);
87 14
        $workspaceUsersService = new WorkspaceUsersService($requestManager, $hydrator);
88
89 14
        $toggl = new Toggl(
90 14
            $authenticationService,
91 14
            $clientService,
92 14
            $dashboardService,
93 14
            $projectService,
94 14
            $projectUsersService,
95 14
            $tagService,
96 14
            $taskService,
97 14
            $timeEntryService,
98 14
            $userService,
99 14
            $workspaceService,
100
            $workspaceUsersService
101 14
        );
102
103 14
        return $toggl;
104
    }
105
}
106