forked from ambta/DoctrineEncryptBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstractCommand.php
More file actions
119 lines (103 loc) · 3.28 KB
/
AbstractCommand.php
File metadata and controls
119 lines (103 loc) · 3.28 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php
namespace Ambta\DoctrineEncryptBundle\Command;
use Ambta\DoctrineEncryptBundle\Subscribers\DoctrineEncryptSubscriber;
use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
/**
* Base command containing usefull base methods.
*
* @author Michael Feinbier <michael@feinbier.net>
**/
abstract class AbstractCommand extends ContainerAwareCommand
{
/**
* @var EntityManagerInterface
*/
protected $entityManager;
/**
* @var DoctrineEncryptSubscriber
*/
protected $subscriber;
/**
* @var AnnotationReader
*/
protected $annotationReader;
/**
* {@inheritdoc}
*/
protected function initialize(InputInterface $input, OutputInterface $output)
{
$container = $this->getContainer();
$this->entityManager = $container->get('doctrine.orm.entity_manager');
$this->annotationReader = $container->get('annotation_reader');
$this->subscriber = $container->get('ambta_doctrine_encrypt.subscriber');
}
/**
* Get an result iterator over the whole table of an entity.
*
* @param string $entityName
*
* @return \Doctrine\ORM\Internal\Hydration\IterableResult
*/
protected function getEntityIterator($entityName)
{
$query = $this->entityManager->createQuery(sprintf('SELECT o FROM %s o', $entityName));
return $query->iterate();
}
/**
* Get the number of rows in an entity-table
*
* @param string $entityName
*
* @return int
*/
protected function getTableCount($entityName)
{
$query = $this->entityManager->createQuery(sprintf('SELECT COUNT(o) FROM %s o', $entityName));
return (int) $query->getSingleScalarResult();
}
/**
* Return an array of entity-metadata for all entities
* that have at least one encrypted property.
*
* @return array
*/
protected function getEncryptionableEntityMetaData()
{
$validMetaData = [];
$metaDataArray = $this->entityManager->getMetadataFactory()->getAllMetadata();
foreach ($metaDataArray as $entityMetaData)
{
if ($entityMetaData->isMappedSuperclass) {
continue;
}
$properties = $this->getEncryptionableProperties($entityMetaData);
if (count($properties) == 0) {
continue;
}
$validMetaData[] = $entityMetaData;
}
return $validMetaData;
}
/**
* @param $entityMetaData
*
* @return array
*/
protected function getEncryptionableProperties($entityMetaData)
{
//Create reflectionClass for each meta data object
$reflectionClass = New \ReflectionClass($entityMetaData->name);
$propertyArray = $reflectionClass->getProperties();
$properties = [];
foreach ($propertyArray as $property) {
if ($this->annotationReader->getPropertyAnnotation($property, 'Ambta\DoctrineEncryptBundle\Configuration\Encrypted')) {
$properties[] = $property;
}
}
return $properties;
}
}