Completed
Push — master ( 535b09...0667cb )
by
unknown
56:23
created

LeadFixture::getData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace OroCRM\Bundle\SalesBundle\ImportExport\TemplateFixture;
4
5
use Oro\Bundle\EntityExtendBundle\Tools\ExtendHelper;
6
use Oro\Bundle\ImportExportBundle\TemplateFixture\AbstractTemplateRepository;
7
use Oro\Bundle\ImportExportBundle\TemplateFixture\TemplateFixtureInterface;
8
use OroCRM\Bundle\SalesBundle\Entity\Lead;
9
use OroCRM\Bundle\SalesBundle\Entity\LeadPhone;
10
use OroCRM\Bundle\SalesBundle\Entity\LeadEmail;
11
use OroCRM\Bundle\SalesBundle\Entity\LeadAddress;
12
13
class LeadFixture extends AbstractTemplateRepository implements TemplateFixtureInterface
14
{
15
    /**
16
     * {@inheritdoc}
17
     */
18
    public function getEntityClass()
19
    {
20
        return 'OroCRM\Bundle\SalesBundle\Entity\Lead';
21
    }
22
23
    /**
24
     * {@inheritdoc}
25
     */
26
    public function getData()
27
    {
28
        return $this->getEntityData('Jerry Coleman');
29
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34
    protected function createEntity($key)
35
    {
36
        return new Lead();
37
    }
38
39
    /**
40
     * @param string $key
41
     * @param Lead   $entity
42
     */
43
    public function fillEntityData($key, $entity)
44
    {
45
        $userRepo         = $this->templateManager->getEntityRepository('Oro\Bundle\UserBundle\Entity\User');
46
        $customerRepo     = $this->templateManager->getEntityRepository('OroCRM\Bundle\SalesBundle\Entity\B2bCustomer');
47
        $contactRepo      = $this->templateManager->getEntityRepository('OroCRM\Bundle\ContactBundle\Entity\Contact');
48
        $channelRepo      = $this->templateManager->getEntityRepository('OroCRM\Bundle\ChannelBundle\Entity\Channel');
49
        $organizationRepo = $this->templateManager
50
            ->getEntityRepository('Oro\Bundle\OrganizationBundle\Entity\Organization');
51
52
        switch ($key) {
53
            case 'Jerry Coleman':
54
                $entity->setName('Oro Inc. Lead Name');
55
                $entity->setCompanyName('Oro Inc.');
56
                $entity->setOwner($userRepo->getEntity('John Doo'));
57
                $entity->setOrganization($organizationRepo->getEntity('default'));
58
                $entity->setDataChannel($channelRepo->getEntity('Sales channel|b2b'));
59
                $entity->setCreatedAt(new \DateTime());
60
                $entity->setUpdatedAt(new \DateTime());
61
                $entity->setCustomer($customerRepo->getEntity('Jerry Coleman'));
62
                $entity->setContact($contactRepo->getEntity('Jerry Coleman'));
63
                $entity->addEmail(new LeadEmail('[email protected]'));
64
                $primaryAddress = $this->createLeadAddress(1);
65
                $entity->addAddress($primaryAddress);
66
                $entity->addAddress($this->createLeadAddress(2));
67
                $entity->addAddress($this->createLeadAddress(3));
68
                $entity->setPrimaryAddress($primaryAddress);
69
                $entity->setNamePrefix('Mr.');
70
                $entity->setFirstName('Jerry');
71
                $entity->setLastName('Coleman');
72
                $entity->setNameSuffix('Jr.');
73
                
74
                $statusName = 'New';
75
                $className = ExtendHelper::buildEnumValueClassName(Lead::INTERNAL_STATUS_CODE);
76
                $id = ExtendHelper::buildEnumValueId($statusName);
77
                $entity->setStatus(new $className($id, $statusName));
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class OroCRM\Bundle\SalesBundle\Entity\Lead as the method setStatus() does only exist in the following sub-classes of OroCRM\Bundle\SalesBundle\Entity\Lead: OroCRM\Bundle\SalesBundl...s\Unit\Fixture\LeadStub. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
78
                
79
                $entity->setJobTitle('Manager');
80
                $entity->addPhone(new LeadPhone('585-255-1127'));
81
                $entity->addPhone(new LeadPhone('978-242-1314'));
82
                $entity->setWebsite('http://orocrm.com');
83
                $entity->setNumberOfEmployees(100);
84
                $entity->setIndustry('Internet');
85
86
                return;
87
        }
88
89
        parent::fillEntityData($key, $entity);
90
    }
91
92
    /**
93
     * @param int $number
94
     *
95
     * @return LeadAddress
96
     *
97
     * @throws \LogicException
98
     */
99
    protected function createLeadAddress($number)
100
    {
101
        $countryRepo = $this->templateManager
102
            ->getEntityRepository('Oro\Bundle\AddressBundle\Entity\Country');
103
        $regionRepo  = $this->templateManager
104
            ->getEntityRepository('Oro\Bundle\AddressBundle\Entity\Region');
105
106
        $entity = new LeadAddress();
107
108
        $entity
109
            ->setFirstName('Jerry')
110
            ->setLastName('Coleman');
111
112 View Code Duplication
        switch ($number) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
113
            case 1:
114
                $entity
115
                    ->setCity('Rochester')
116
                    ->setStreet('1215 Caldwell Road')
117
                    ->setPostalCode('14608')
118
                    ->setRegion($regionRepo->getEntity('NY'))
119
                    ->setCountry($countryRepo->getEntity('US'));
120
                break;
121
            case 2:
122
                $entity
123
                    ->setCity('New York')
124
                    ->setStreet('4677 Pallet Street')
125
                    ->setPostalCode('10011')
126
                    ->setRegion($regionRepo->getEntity('NY'))
127
                    ->setCountry($countryRepo->getEntity('US'));
128
                break;
129
            case 3:
130
                $entity
131
                    ->setCity('New York')
132
                    ->setStreet('52 Jarvisville Road')
133
                    ->setPostalCode('11590')
134
                    ->setRegion($regionRepo->getEntity('NY'))
135
                    ->setCountry($countryRepo->getEntity('US'));
136
                break;
137
            default:
138
                throw new \LogicException(
139
                    sprintf(
140
                        'Undefined lead address. Number: %d.',
141
                        $number
142
                    )
143
                );
144
        }
145
146
        return $entity;
147
    }
148
}
149