Completed
Push — 1.9 ( d8eb28...5c3e2e )
by
unknown
61:52 queued 29s
created

Account::__toString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace OroCRM\Bundle\AccountBundle\Entity;
4
5
use BeSimple\SoapBundle\ServiceDefinition\Annotation as Soap;
6
7
use Doctrine\Common\Collections\ArrayCollection;
8
use Doctrine\Common\Collections\Collection;
9
use Doctrine\ORM\Mapping as ORM;
10
11
use Oro\Bundle\DataAuditBundle\Metadata\Annotation as Oro;
12
use Oro\Bundle\EmailBundle\Model\EmailHolderInterface;
13
use Oro\Bundle\EntityConfigBundle\Metadata\Annotation\Config;
14
use Oro\Bundle\EntityConfigBundle\Metadata\Annotation\ConfigField;
15
use Oro\Bundle\LocaleBundle\Model\NameInterface;
16
use Oro\Bundle\OrganizationBundle\Entity\Organization;
17
use Oro\Bundle\UserBundle\Entity\User;
18
19
use OroCRM\Bundle\AccountBundle\Model\ExtendAccount;
20
use OroCRM\Bundle\ContactBundle\Entity\Contact;
21
22
/**
23
 * @ORM\Entity()
24
 * @ORM\Table(name="orocrm_account", indexes={@ORM\Index(name="account_name_idx", columns={"name"})})
25
 * @ORM\HasLifecycleCallbacks()
26
 * @Oro\Loggable
27
 * @Config(
28
 *      routeName="orocrm_account_index",
29
 *      routeView="orocrm_account_view",
30
 *      defaultValues={
31
 *          "entity"={
32
 *              "icon"="icon-suitcase"
33
 *          },
34
 *          "ownership"={
35
 *              "owner_type"="USER",
36
 *              "owner_field_name"="owner",
37
 *              "owner_column_name"="user_owner_id",
38
 *              "organization_field_name"="organization",
39
 *              "organization_column_name"="organization_id"
40
 *          },
41
 *          "security"={
42
 *              "type"="ACL",
43
 *              "group_name"=""
44
 *          },
45
 *          "merge"={
46
 *              "enable"=true
47
 *          },
48
 *          "form"={
49
 *              "form_type"="orocrm_account_select",
50
 *              "grid_name"="accounts-select-grid",
51
 *          },
52
 *          "dataaudit"={
53
 *              "auditable"=true
54
 *          },
55
 *          "grid"={
56
 *              "default"="accounts-grid",
57
 *              "context"="accounts-for-context-grid"
58
 *          },
59
 *          "tag"={
60
 *              "enabled"=true
61
 *          }
62
 *      }
63
 * )
64
 */
65
class Account extends ExtendAccount implements EmailHolderInterface, NameInterface
66
{
67
    /**
68
     * @ORM\Id
69
     * @ORM\Column(type="integer")
70
     * @ORM\GeneratedValue(strategy="AUTO")
71
     * @Soap\ComplexType("int", nillable=true)
72
     * @ConfigField(
73
     *      defaultValues={
74
     *          "importexport"={
75
     *              "order"=10
76
     *          }
77
     *      }
78
     * )
79
     */
80
    protected $id;
81
82
    /**
83
     * @var string
84
     *
85
     * @ORM\Column(type="string", length=255)
86
     * @Soap\ComplexType("string")
87
     * @Oro\Versioned
88
     * @ConfigField(
89
     *      defaultValues={
90
     *          "merge"={
91
     *              "display"=true
92
     *          },
93
     *          "dataaudit"={
94
     *              "auditable"=true
95
     *          },
96
     *          "importexport"={
97
     *              "identity"=true,
98
     *              "order"=20
99
     *          }
100
     *      }
101
     * )
102
     */
103
    protected $name;
104
105
    /**
106
     * @var User
107
     * @ORM\ManyToOne(targetEntity="Oro\Bundle\UserBundle\Entity\User")
108
     * @ORM\JoinColumn(name="user_owner_id", referencedColumnName="id", onDelete="SET NULL")
109
     * @Soap\ComplexType("string", nillable=true)
110
     * @Oro\Versioned
111
     * @ConfigField(
112
     *      defaultValues={
113
     *          "merge"={
114
     *              "display"=true
115
     *          },
116
     *          "dataaudit"={
117
     *              "auditable"=true
118
     *          },
119
     *          "importexport"={
120
     *              "order"=30,
121
     *              "short"=true
122
     *          }
123
     *      }
124
     * )
125
     */
126
    protected $owner;
127
128
    /**
129
     * Contacts storage
130
     *
131
     * @var ArrayCollection $contacts
132
     *
133
     * @ORM\ManyToMany(targetEntity="OroCRM\Bundle\ContactBundle\Entity\Contact", inversedBy="accounts")
134
     * @ORM\JoinTable(name="orocrm_account_to_contact")
135
     * @ConfigField(
136
     *      defaultValues={
137
     *          "merge"={
138
     *              "display"=true
139
     *          },
140
     *          "importexport"={
141
     *              "order"=50,
142
     *              "short"=true
143
     *          }
144
     *      }
145
     * )
146
     */
147
    protected $contacts;
148
149
    /**
150
     * Default contact entity
151
     *
152
     * @var Contact
153
     *
154
     * @ORM\ManyToOne(targetEntity="OroCRM\Bundle\ContactBundle\Entity\Contact", inversedBy="defaultInAccounts")
155
     * @ORM\JoinColumn(name="default_contact_id", referencedColumnName="id", onDelete="SET NULL")
156
     * @ConfigField(
157
     *      defaultValues={
158
     *          "merge"={
159
     *              "display"=true
160
     *          },
161
     *          "importexport"={
162
     *              "order"=40,
163
     *              "short"=true
164
     *          }
165
     *      }
166
     * )
167
     */
168
    protected $defaultContact;
169
170
    /**
171
     * @var \DateTime
172
     *
173
     * @ORM\Column(type="datetime")
174
     * @Soap\ComplexType("dateTime", nillable=true)
175
     * @ConfigField(
176
     *      defaultValues={
177
     *          "entity"={
178
     *              "label"="oro.ui.created_at"
179
     *          },
180
     *          "importexport"={
181
     *              "excluded"=true
182
     *          }
183
     *      }
184
     * )
185
     */
186
    protected $createdAt;
187
188
    /**
189
     * @var \DateTime
190
     *
191
     * @ORM\Column(type="datetime")
192
     * @Soap\ComplexType("dateTime", nillable=true)
193
     * @ConfigField(
194
     *      defaultValues={
195
     *          "entity"={
196
     *              "label"="oro.ui.updated_at"
197
     *          },
198
     *          "importexport"={
199
     *              "excluded"=true
200
     *          }
201
     *      }
202
     * )
203
     */
204
    protected $updatedAt;
205
206
    /**
207
     * @var Organization
208
     *
209
     * @ORM\ManyToOne(targetEntity="Oro\Bundle\OrganizationBundle\Entity\Organization")
210
     * @ORM\JoinColumn(name="organization_id", referencedColumnName="id", onDelete="SET NULL")
211
     */
212
    protected $organization;
213
214
    /**
215
     * @var Account
216
     * @ORM\ManyToOne(targetEntity="Account")
217
     * @ORM\JoinColumn(name="referred_by_id", referencedColumnName="id", onDelete="SET NULL")
218
     */
219
    protected $referredBy;
220
221
    public function __construct()
222
    {
223
        parent::__construct();
224
225
        $this->contacts = new ArrayCollection();
226
    }
227
228
    /**
229
     * Returns the account unique id.
230
     *
231
     * @return mixed
232
     */
233
    public function getId()
234
    {
235
        return $this->id;
236
    }
237
238
    /**
239
     * @param  int     $id
240
     * @return Account
241
     */
242
    public function setId($id)
243
    {
244
        $this->id = $id;
245
246
        return $this;
247
    }
248
249
    /**
250
     * {@inheritDoc}
251
     */
252
    public function getName()
253
    {
254
        return $this->name;
255
    }
256
257
    /**
258
     * Set account name
259
     *
260
     * @param string $name New name
261
     *
262
     * @return Account
263
     */
264
    public function setName($name)
265
    {
266
        $this->name = $name;
267
268
        return $this;
269
    }
270
271
    /**
272
     * Get created date/time
273
     *
274
     * @return \DateTime
275
     */
276
    public function getCreatedAt()
277
    {
278
        return $this->createdAt;
279
    }
280
281
    /**
282
     * @param \DateTime
283
     *
284
     * @return Account
285
     */
286
    public function setCreatedAt($created)
287
    {
288
        $this->createdAt = $created;
289
290
        return $this;
291
    }
292
293
    /**
294
     * Get last update date/time
295
     *
296
     * @return \DateTime
297
     */
298
    public function getUpdatedAt()
299
    {
300
        return $this->updatedAt;
301
    }
302
303
    /**
304
     * @param \DateTime
305
     *
306
     * @return Account
307
     */
308
    public function setUpdatedAt($updated)
309
    {
310
        $this->updatedAt = $updated;
311
312
        return $this;
313
    }
314
315
    /**
316
     * Get contacts collection
317
     *
318
     * @return Collection|Contact[]
319
     */
320
    public function getContacts()
321
    {
322
        return $this->contacts;
323
    }
324
325
    /**
326
     * Add specified contact
327
     *
328
     * @param Contact $contact
329
     *
330
     * @return Account
331
     */
332
    public function addContact(Contact $contact)
333
    {
334
        if (!$this->getContacts()->contains($contact)) {
335
            $this->getContacts()->add($contact);
336
            $contact->addAccount($this);
337
        }
338
339
        return $this;
340
    }
341
342
    /**
343
     * Set contacts collection
344
     *
345
     * @param Collection $contacts
346
     *
347
     * @return Account
348
     */
349
    public function setContacts(Collection $contacts)
350
    {
351
        $this->contacts = $contacts;
0 ignored issues
show
Documentation Bug introduced by
$contacts is of type object<Doctrine\Common\Collections\Collection>, but the property $contacts was declared to be of type object<Doctrine\Common\C...ctions\ArrayCollection>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
352
353
        return $this;
354
    }
355
356
    /**
357
     * Remove specified contact
358
     *
359
     * @param Contact $contact
360
     *
361
     * @return Account
362
     */
363
    public function removeContact(Contact $contact)
364
    {
365
        if ($this->getContacts()->contains($contact)) {
366
            $this->getContacts()->removeElement($contact);
367
            $contact->removeAccount($this);
368
        }
369
370
        return $this;
371
    }
372
373
    public function __toString()
374
    {
375
        return (string) $this->getName();
376
    }
377
378
    /**
379
     * Pre persist event listener
380
     *
381
     * @ORM\PrePersist
382
     */
383
    public function beforeSave()
384
    {
385
        $this->createdAt = new \DateTime('now', new \DateTimeZone('UTC'));
386
        $this->updatedAt = new \DateTime('now', new \DateTimeZone('UTC'));
387
    }
388
389
    /**
390
     * Pre update event handler
391
     *
392
     * @ORM\PreUpdate
393
     */
394
    public function doPreUpdate()
395
    {
396
        $this->updatedAt = new \DateTime('now', new \DateTimeZone('UTC'));
397
    }
398
399
    /**
400
     * @return User
401
     */
402
    public function getOwner()
403
    {
404
        return $this->owner;
405
    }
406
407
    /**
408
     * @param User $owningUser
409
     *
410
     * @return Account
411
     */
412
    public function setOwner($owningUser)
413
    {
414
        $this->owner = $owningUser;
415
416
        return $this;
417
    }
418
419
    /**
420
     * @param Contact $defaultContact
421
     *
422
     * @return Account
423
     */
424
    public function setDefaultContact($defaultContact)
425
    {
426
        if ($this->defaultContact === $defaultContact) {
427
            return $this;
428
        }
429
430
        /**
431
         * As resolving of $this->defaultContact->getDefaultInAccounts() lazy collection will
432
         * overwrite $this->defaultContact to value from db, make sure the collection is resolved
433
         */
434
        if ($this->defaultContact) {
435
            $this->defaultContact->getDefaultInAccounts()->toArray();
436
        }
437
438
        $originalContact = $this->defaultContact;
439
        $this->defaultContact = $defaultContact;
440
441
        if ($defaultContact) {
442
            $defaultContact->addDefaultInAccount($this);
443
        }
444
445
        if ($originalContact) {
446
            $originalContact->removeDefaultInAccount($this);
447
        }
448
449
        if ($defaultContact && !$this->contacts->contains($defaultContact)) {
450
            $this->addContact($defaultContact);
451
        }
452
453
        return $this;
454
    }
455
456
    /**
457
     * @return Contact
458
     */
459
    public function getDefaultContact()
460
    {
461
        return $this->defaultContact;
462
    }
463
464
    /**
465
     * Get the primary email address of the default contact
466
     *
467
     * @return string
468
     */
469
    public function getEmail()
470
    {
471
        $contact = $this->getDefaultContact();
472
        if (!$contact) {
473
            return null;
474
        }
475
476
        return $contact->getEmail();
477
    }
478
479
    /**
480
     * Set organization
481
     *
482
     * @param Organization $organization
483
     *
484
     * @return Account
485
     */
486
    public function setOrganization(Organization $organization = null)
487
    {
488
        $this->organization = $organization;
489
490
        return $this;
491
    }
492
493
    /**
494
     * Get organization
495
     *
496
     * @return Organization
497
     */
498
    public function getOrganization()
499
    {
500
        return $this->organization;
501
    }
502
503
    /**
504
     * @return Account
505
     */
506
    public function getReferredBy()
507
    {
508
        return $this->referredBy;
509
    }
510
511
    /**
512
     * @param Account $referredBy
513
     *
514
     * @return Account
515
     */
516
    public function setReferredBy(Account $referredBy = null)
517
    {
518
        $this->referredBy = $referredBy;
519
520
        return $this;
521
    }
522
}
523