This repository has been archived by the owner on Aug 4, 2023. It is now read-only.
/
AbstractSyncer.php
60 lines (50 loc) · 1.79 KB
/
AbstractSyncer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php
declare(strict_types=1);
/*
* This file is part of the Serendipity HQ Stripe Bundle.
*
* Copyright (c) Adamo Aerendir Crespi <aerendir@serendipityhq.com>.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace SerendipityHQ\Bundle\StripeBundle\Syncer;
use Doctrine\ORM\EntityManagerInterface;
use SerendipityHQ\Bundle\StripeBundle\Model\StripeLocalCustomer;
/**
* An abstract class that all helpers extend.
*
* @author Adamo Crespi <hello@aerendir.me>
*/
abstract class AbstractSyncer implements SyncerInterface
{
/** @var EntityManagerInterface $entityManager */
private $entityManager;
public function __construct(EntityManagerInterface $entityManager)
{
$this->entityManager = $entityManager;
}
public function getEntityManager(): EntityManagerInterface
{
return $this->entityManager;
}
/**
* Gets the local customer object searching for it in the database or in the newly created entities persisted but
* not yet flushed.
*
* @param $stripeCustomerId
*
* @return bool|StripeLocalCustomer false if the StripeLocalCustomer was not found
*/
protected function getLocalCustomer($stripeCustomerId)
{
// First try to get the customer from the database
$localCustomer = $this->getEntityManager()->getRepository(StripeLocalCustomer::class)->findOneByStripeId($stripeCustomerId);
// If we found it, return it
if (null !== $localCustomer) {
return $localCustomer;
}
// Try to find the customer in the newly created one that are not already persisted
return $this->getEntityManager()->getUnitOfWork()->tryGetById($stripeCustomerId, StripeLocalCustomer::class);
}
}