1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Badger\Bundle\GameBundle\Doctrine\Saver; |
4
|
|
|
|
5
|
|
|
use Badger\Component\StorageUtils\Saver\SaverInterface; |
6
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
7
|
|
|
use Doctrine\Common\Util\ClassUtils; |
8
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @author Adrien Pétremann <[email protected]> |
12
|
|
|
* @license http://opensource.org/licenses/MIT The MIT License (MIT) |
13
|
|
|
*/ |
14
|
|
View Code Duplication |
class BadgeCompletionSaver implements SaverInterface |
|
|
|
|
15
|
|
|
{ |
16
|
|
|
/** @var ObjectManager */ |
17
|
|
|
protected $objectManager; |
18
|
|
|
|
19
|
|
|
/** @var EventDispatcherInterface */ |
20
|
|
|
protected $eventDispatcher; |
21
|
|
|
|
22
|
|
|
/** @var string */ |
23
|
|
|
protected $savedClass; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param ObjectManager $objectManager |
27
|
|
|
* @param EventDispatcherInterface $eventDispatcher |
28
|
|
|
* @param string $savedClass |
29
|
|
|
*/ |
30
|
|
|
public function __construct( |
31
|
|
|
ObjectManager $objectManager, |
32
|
|
|
EventDispatcherInterface $eventDispatcher, |
33
|
|
|
$savedClass |
34
|
|
|
) { |
35
|
|
|
$this->objectManager = $objectManager; |
36
|
|
|
$this->savedClass = $savedClass; |
37
|
|
|
$this->eventDispatcher = $eventDispatcher; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* {@inheritdoc} |
42
|
|
|
*/ |
43
|
|
|
public function save($badgeCompletion, array $options = []) |
44
|
|
|
{ |
45
|
|
|
if (false === ($badgeCompletion instanceof $this->savedClass)) { |
46
|
|
|
throw new \InvalidArgumentException( |
47
|
|
|
sprintf( |
48
|
|
|
'Expects a "%s", "%s" provided.', |
49
|
|
|
$this->savedClass, |
50
|
|
|
ClassUtils::getRealClass($badgeCompletion) |
51
|
|
|
) |
52
|
|
|
); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$this->objectManager->persist($badgeCompletion); |
56
|
|
|
$this->objectManager->flush(); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
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.