Completed
Push — 1.10 ( 3f8f95...f007bc )
by
unknown
09:16
created

LoadCampaignByCloseRevenueWidgetFixture   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 131
Duplicated Lines 37.4 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
lcom 1
cbo 9
dl 49
loc 131
rs 10
c 1
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A createLead() 16 16 2
A createOpportunity() 0 20 2
A createCampaign() 14 14 2
A createOpportunities() 0 21 1
A load() 19 19 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
namespace OroCRM\Bundle\SalesBundle\Tests\Functional\Fixture;
3
4
use Doctrine\Common\DataFixtures\AbstractFixture;
5
use Doctrine\Common\Persistence\ObjectManager;
6
7
use Oro\Bundle\DashboardBundle\Entity\Dashboard;
8
use Oro\Bundle\DashboardBundle\Entity\Widget;
9
use Oro\Bundle\EntityExtendBundle\Tools\ExtendHelper;
10
use Oro\Bundle\OrganizationBundle\Entity\Organization;
11
12
use OroCRM\Bundle\CampaignBundle\Entity\Campaign;
13
use OroCRM\Bundle\SalesBundle\Entity\Lead;
14
use OroCRM\Bundle\SalesBundle\Entity\Opportunity;
15
16
class LoadCampaignByCloseRevenueWidgetFixture extends AbstractFixture
17
{
18
    /**
19
     * @var Organization
20
     */
21
    protected $organization;
22
    /**
23
     * @var ObjectManager
24
     */
25
    protected $em;
26
27
    private $opportunityCount = 0;
28
29
    /**
30
     * @param string    $name
31
     * @param Campaign  $campaign
32
     * @param string    $referenceName
33
     *
34
     * @return Lead
35
     */
36 View Code Duplication
    protected function createLead(
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...
37
        $name,
38
        Campaign $campaign,
39
        $referenceName = null
40
    ) {
41
        $lead = new Lead();
42
        $lead->setName($name);
43
        $lead->setOrganization($this->organization);
44
        $lead->setCampaign($campaign);
0 ignored issues
show
Bug introduced by
The method setCampaign() does not seem to exist on object<OroCRM\Bundle\SalesBundle\Entity\Lead>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
45
        $this->em->persist($lead);
46
        $this->em->flush();
47
48
        ($referenceName === null ) ?: $this->setReference($referenceName, $lead);
49
50
        return $lead;
51
    }
52
53
    /**
54
     * @param \DateTime $createdAt
55
     * @param string    $status
56
     * @param Lead      $lead
57
     * @param int       $closeRevenue
58
     */
59
    protected function createOpportunity(
60
        $createdAt,
61
        $status,
62
        Lead $lead,
63
        $closeRevenue = null
64
    ) {
65
        $className = ExtendHelper::buildEnumValueClassName(Opportunity::INTERNAL_STATUS_CODE);
66
        $opportunityStatus = $this->em->getRepository($className)->find(ExtendHelper::buildEnumValueId($status));
67
68
        $opportunity = new Opportunity();
69
        $opportunity->setName(sprintf('Test Opportunity #%d', ++$this->opportunityCount));
70
        $opportunity->setStatus($opportunityStatus);
71
        $opportunity->setLead($lead);
72
        ($closeRevenue === null ) ?: $opportunity->setCloseRevenue($closeRevenue);
73
        $opportunity->setOrganization($this->organization);
74
        $this->em->persist($opportunity);
75
76
        $opportunity->setCreatedAt($createdAt);
77
        $this->em->flush();
78
    }
79
80
    /**
81
     * @param string $name
82
     * @param string $code
83
     * @param string $reference
84
     *
85
     * @return Campaign
86
     */
87 View Code Duplication
    protected function createCampaign($name, $code, $reference = null)
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
    {
89
        $campaign = new Campaign();
90
        $campaign->setName($name);
91
        $campaign->setCode($code);
92
        $campaign->setOrganization($this->organization);
93
        $campaign->setReportPeriod(Campaign::PERIOD_MONTHLY);
94
        $this->em->persist($campaign);
95
        $this->em->flush();
96
97
        ($reference === null) ?: $this->setReference($reference, $campaign);
98
99
        return $campaign;
100
    }
101
102
    protected function createOpportunities()
103
    {
104
        $createdAt = new \DateTime('2016-12-28 12:03:10', new \DateTimeZone('UTC'));
105
106
        $defaultCampaign = $this->createCampaign('Default campaing', 'cmt');
107
        $anotherCampaign = $this->createCampaign('Another campaing', 'test');
108
109
        $defaultLead = $this->createLead('Default Lead', $defaultCampaign);
110
        $anotherLead = $this->createLead('Another Lead', $anotherCampaign);
111
112
        // Every opportunity has value of $100
113
        $this->createOpportunity($createdAt, 'won', $defaultLead, 100);
114
        $this->createOpportunity($createdAt, 'in_progress', $defaultLead, 100);
115
        $this->createOpportunity($createdAt, 'lost', $defaultLead, 100);
116
117
        //This opportunity without close revenue
118
        $this->createOpportunity($createdAt, 'won', $anotherLead);
119
120
        $createdAt->add(new \DateInterval('P1D'));
121
        $this->createOpportunity($createdAt, 'won', $defaultLead, 100);
122
    }
123
124
    /**
125
     * @inheritDoc
126
     */
127 View Code Duplication
    public function load(ObjectManager $manager)
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...
128
    {
129
        $this->organization = $manager->getRepository('OroOrganizationBundle:Organization')->getFirst();
130
        $this->em = $manager;
131
        $this->createOpportunities();
132
        $dashboard = new Dashboard();
133
        $dashboard->setName('dashboard');
134
        $campaignLeadsWidget = new Widget();
135
        $campaignLeadsWidget
136
            ->setDashboard($dashboard)
137
            ->setName('campaigns_by_close_revenue')
138
            ->setLayoutPosition([1, 1]);
139
        $dashboard->addWidget($campaignLeadsWidget);
140
        if (!$this->hasReference('widget_campaigns_by_close_revenue')) {
141
            $this->setReference('widget_campaigns_by_close_revenue', $campaignLeadsWidget);
142
        }
143
        $manager->persist($dashboard);
144
        $manager->flush();
145
    }
146
}
147