EditProjectHandlerSpec   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 82
Duplicated Lines 80.49 %

Coupling/Cohesion

Components 0
Dependencies 11

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 11
dl 66
loc 82
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A let() 0 4 1
A it_is_initializable() 0 4 1
A it_edits_project() 20 20 1
A it_edits_project_with_default_slug() 20 20 1
A it_does_not_allow_to_edit_project_if_project_does_not_exist() 8 8 1
A it_does_not_allow_to_edit_project_if_editor_is_not_organization_owner() 18 18 1

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
/*
4
 * This file is part of the Kreta package.
5
 *
6
 * (c) Beñat Espiña <[email protected]>
7
 * (c) Gorka Laucirica <[email protected]>
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
declare(strict_types=1);
14
15
namespace Spec\Kreta\TaskManager\Application\Command\Project;
16
17
use Kreta\SharedKernel\Domain\Model\Identity\Slug;
18
use Kreta\TaskManager\Application\Command\Project\EditProjectCommand;
19
use Kreta\TaskManager\Application\Command\Project\EditProjectHandler;
20
use Kreta\TaskManager\Domain\Model\Organization\Organization;
21
use Kreta\TaskManager\Domain\Model\Organization\OrganizationId;
22
use Kreta\TaskManager\Domain\Model\Organization\OrganizationRepository;
23
use Kreta\TaskManager\Domain\Model\Project\Project;
24
use Kreta\TaskManager\Domain\Model\Project\ProjectDoesNotExistException;
25
use Kreta\TaskManager\Domain\Model\Project\ProjectId;
26
use Kreta\TaskManager\Domain\Model\Project\ProjectName;
27
use Kreta\TaskManager\Domain\Model\Project\ProjectRepository;
28
use Kreta\TaskManager\Domain\Model\Project\UnauthorizedProjectActionException;
29
use Kreta\TaskManager\Domain\Model\User\UserId;
30
use PhpSpec\ObjectBehavior;
31
use Prophecy\Argument;
32
33
class EditProjectHandlerSpec extends ObjectBehavior
34
{
35
    function let(ProjectRepository $projectRepository, OrganizationRepository $organizationRepository)
36
    {
37
        $this->beConstructedWith($projectRepository, $organizationRepository);
38
    }
39
40
    function it_is_initializable()
41
    {
42
        $this->shouldHaveType(EditProjectHandler::class);
43
    }
44
45 View Code Duplication
    function it_edits_project(
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...
46
        OrganizationRepository $organizationRepository,
47
        ProjectRepository $projectRepository,
48
        EditProjectCommand $command,
49
        Project $project,
50
        Organization $organization,
51
        OrganizationId $organizationId
52
    ) {
53
        $command->id()->willReturn('project-id');
54
        $command->name()->willReturn('Project name');
55
        $command->slug()->willReturn('project-name');
56
        $command->editorId()->willReturn('editor-id');
57
        $projectRepository->projectOfId(ProjectId::generate('project-id'))->shouldBeCalled()->willReturn($project);
58
        $project->organizationId()->shouldBeCalled()->willReturn($organizationId);
59
        $organizationRepository->organizationOfId($organizationId)->shouldBeCalled()->willReturn($organization);
60
        $organization->isOwner(UserId::generate('editor-id'))->shouldBeCalled()->willReturn(true);
61
        $project->edit(new ProjectName('Project name'), new Slug('project-name'))->shouldBeCalled();
62
        $projectRepository->persist(Argument::type(Project::class))->shouldBeCalled();
63
        $this->__invoke($command);
64
    }
65
66 View Code Duplication
    function it_edits_project_with_default_slug(
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...
67
        OrganizationRepository $organizationRepository,
68
        ProjectRepository $projectRepository,
69
        EditProjectCommand $command,
70
        Project $project,
71
        Organization $organization,
72
        OrganizationId $organizationId
73
    ) {
74
        $command->id()->willReturn('project-id');
75
        $command->name()->willReturn('Project name');
76
        $command->slug()->willReturn(null);
77
        $command->editorId()->willReturn('editor-id');
78
        $projectRepository->projectOfId(ProjectId::generate('project-id'))->shouldBeCalled()->willReturn($project);
79
        $project->organizationId()->shouldBeCalled()->willReturn($organizationId);
80
        $organizationRepository->organizationOfId($organizationId)->shouldBeCalled()->willReturn($organization);
81
        $organization->isOwner(UserId::generate('editor-id'))->shouldBeCalled()->willReturn(true);
82
        $project->edit(new ProjectName('Project name'), new Slug('Project name'))->shouldBeCalled();
83
        $projectRepository->persist(Argument::type(Project::class))->shouldBeCalled();
84
        $this->__invoke($command);
85
    }
86
87 View Code Duplication
    function it_does_not_allow_to_edit_project_if_project_does_not_exist(
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...
88
        ProjectRepository $projectRepository,
89
        EditProjectCommand $command
90
    ) {
91
        $command->id()->willReturn('project-id');
92
        $projectRepository->projectOfId(ProjectId::generate('project-id'))->shouldBeCalled()->willReturn(null);
93
        $this->shouldThrow(ProjectDoesNotExistException::class)->during__invoke($command);
94
    }
95
96 View Code Duplication
    function it_does_not_allow_to_edit_project_if_editor_is_not_organization_owner(
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...
97
        OrganizationRepository $organizationRepository,
98
        ProjectRepository $projectRepository,
99
        EditProjectCommand $command,
100
        Project $project,
101
        Organization $organization,
102
        OrganizationId $organizationId
103
    ) {
104
        $command->id()->willReturn('project-id');
105
        $command->name()->willReturn('Project name');
106
        $command->slug()->willReturn(null);
107
        $command->editorId()->willReturn('editor-id');
108
        $projectRepository->projectOfId(ProjectId::generate('project-id'))->shouldBeCalled()->willReturn($project);
109
        $project->organizationId()->shouldBeCalled()->willReturn($organizationId);
110
        $organizationRepository->organizationOfId($organizationId)->shouldBeCalled()->willReturn($organization);
111
        $organization->isOwner(UserId::generate('editor-id'))->shouldBeCalled()->willReturn(false);
112
        $this->shouldThrow(UnauthorizedProjectActionException::class)->during__invoke($command);
113
    }
114
}
115