1
|
|
|
<?php |
2
|
|
|
namespace OroCRM\Bundle\SalesBundle\Tests\Functional\Fixture; |
3
|
|
|
|
4
|
|
|
use Doctrine\Common\DataFixtures\AbstractFixture; |
5
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
6
|
|
|
use Oro\Bundle\DashboardBundle\Entity\Dashboard; |
7
|
|
|
use Oro\Bundle\DashboardBundle\Entity\Widget; |
8
|
|
|
use Oro\Bundle\EntityExtendBundle\Tools\ExtendHelper; |
9
|
|
|
use Oro\Bundle\OrganizationBundle\Entity\Organization; |
10
|
|
|
use OroCRM\Bundle\SalesBundle\Entity\Opportunity; |
11
|
|
|
|
12
|
|
|
class LoadOpportunityByStatusWidgetFixture extends AbstractFixture |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var Organization |
16
|
|
|
*/ |
17
|
|
|
protected $organization; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var ObjectManager |
21
|
|
|
*/ |
22
|
|
|
protected $em; |
23
|
|
|
|
24
|
|
View Code Duplication |
protected function createOpportunities() |
|
|
|
|
25
|
|
|
{ |
26
|
|
|
$createdAt = new \DateTime('2016-12-28 12:03:10', new \DateTimeZone('UTC')); |
27
|
|
|
for ($i = 1; $i < 4; $i++) { |
28
|
|
|
$this->createOpportunity($createdAt, $i); |
29
|
|
|
$createdAt->add(new \DateInterval('P1D')); |
30
|
|
|
} |
31
|
|
|
//insert one opportunity for previous months |
32
|
|
|
$createdAt = new \DateTime('now', new \DateTimeZone('UTC')); |
33
|
|
|
$this->createOpportunity($createdAt, ++$i); |
34
|
|
|
$createdAt->sub(new \DateInterval('P2M')); |
35
|
|
|
$this->createOpportunity($createdAt, ++$i); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param $createdAt |
40
|
|
|
* @param $id |
41
|
|
|
*/ |
42
|
|
|
protected function createOpportunity($createdAt, $id) |
43
|
|
|
{ |
44
|
|
|
$className = ExtendHelper::buildEnumValueClassName(Opportunity::INTERNAL_STATUS_CODE); |
45
|
|
|
$openStatus = $this->em->getRepository($className)->find(ExtendHelper::buildEnumValueId('in_progress')); |
46
|
|
|
$opportunity = new Opportunity(); |
47
|
|
|
$opportunity->setName('name '.$id); |
48
|
|
|
$opportunity->setStatus($openStatus); |
49
|
|
|
$opportunity->setOrganization($this->organization); |
50
|
|
|
$this->em->persist($opportunity); |
51
|
|
|
$opportunity->setCreatedAt($createdAt); |
52
|
|
|
$this->em->flush(); |
53
|
|
|
} |
54
|
|
|
/** |
55
|
|
|
* @inheritDoc |
56
|
|
|
*/ |
57
|
|
View Code Duplication |
public function load(ObjectManager $manager) |
|
|
|
|
58
|
|
|
{ |
59
|
|
|
$this->organization = $manager->getRepository('OroOrganizationBundle:Organization')->getFirst(); |
60
|
|
|
$this->em = $manager; |
61
|
|
|
$this->createOpportunities(); |
62
|
|
|
$dashboard = new Dashboard(); |
63
|
|
|
$dashboard->setName('dashboard'); |
64
|
|
|
$opportunityByStatusWidget = new Widget(); |
65
|
|
|
$opportunityByStatusWidget |
66
|
|
|
->setDashboard($dashboard) |
67
|
|
|
->setName('opportunities_by_state') |
68
|
|
|
->setLayoutPosition([1, 1]); |
69
|
|
|
$dashboard->addWidget($opportunityByStatusWidget); |
70
|
|
|
if (!$this->hasReference('widget_opportunity_by_status')) { |
71
|
|
|
$this->setReference('widget_opportunity_by_status', $opportunityByStatusWidget); |
72
|
|
|
} |
73
|
|
|
$manager->persist($dashboard); |
74
|
|
|
$manager->flush(); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
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.