These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Kunstmaan\TranslatorBundle\Service\Migrations; |
||
4 | |||
5 | use Doctrine\ORM\EntityManager; |
||
6 | |||
7 | class MigrationsService |
||
8 | { |
||
9 | /** |
||
10 | * Repository for Translations |
||
11 | * |
||
12 | * @var \Kunstmaan\TranslatorBundle\Repository\TranslationRepository |
||
13 | */ |
||
14 | private $translationRepository; |
||
15 | |||
16 | /** |
||
17 | * @var EntityManager |
||
18 | */ |
||
19 | private $entityManager; |
||
20 | |||
21 | public function getDiffSqlArray() |
||
22 | { |
||
23 | $sql = array(); |
||
24 | $inserts = $this->getNewTranslationSql(); |
||
25 | $updates = $this->getUpdatedTranslationSqlArray(); |
||
26 | |||
27 | if ($inserts != '' && !is_null($inserts)) { |
||
28 | $sql[] = $inserts; |
||
29 | } |
||
30 | |||
31 | if (count($updates) > 0 && is_array($updates)) { |
||
32 | $sql = array_merge($sql, $updates); |
||
33 | } |
||
34 | |||
35 | return $sql; |
||
36 | } |
||
37 | |||
38 | public function getUpdatedTranslationSqlArray() |
||
39 | { |
||
40 | $ignoreFields = array('id'); |
||
41 | $uniqueKeys = array('domain', 'locale', 'keyword'); |
||
42 | |||
43 | $translations = $this->translationRepository->findBy(array('flag' => \Kunstmaan\TranslatorBundle\Entity\Translation::FLAG_UPDATED)); |
||
44 | |||
45 | if (count($translations) <= 0) { |
||
46 | return array(); |
||
47 | } |
||
48 | |||
49 | $fieldNames = $this->entityManager->getClassMetadata('\Kunstmaan\TranslatorBundle\Entity\Translation')->getFieldNames(); |
||
50 | $tableName = $this->entityManager->getClassMetadata('\Kunstmaan\TranslatorBundle\Entity\Translation')->getTableName(); |
||
51 | $tableName = $this->entityManager->getConnection()->quoteIdentifier($tableName); |
||
52 | |||
53 | $fieldNames = array_diff($fieldNames, $ignoreFields, $uniqueKeys); |
||
54 | |||
55 | $sql = array(); |
||
56 | |||
57 | foreach ($translations as $translation) { |
||
58 | $updateValues = array(); |
||
59 | $whereValues = array(); |
||
60 | |||
61 | foreach ($fieldNames as $fieldName) { |
||
62 | $value = $translation->{'get'.$fieldName}(); |
||
63 | $columnName = $this->entityManager->getClassMetadata('\Kunstmaan\TranslatorBundle\Entity\Translation')->getColumnName($fieldName); |
||
64 | if ($value instanceof \DateTime) { |
||
65 | $value = $value->format('Y-m-d H:i:s'); |
||
66 | } |
||
67 | |||
68 | $updateValues[] = $this->entityManager->getConnection()->quoteIdentifier($columnName) . ' = ' . $this->entityManager->getConnection()->quote($value); |
||
69 | } |
||
70 | |||
71 | foreach ($uniqueKeys as $uniqueKey) { |
||
72 | $value = $translation->{'get'.$uniqueKey}(); |
||
73 | |||
74 | if ($value instanceof \DateTime) { |
||
75 | $value = $value->format('Y-m-d H:i:s'); |
||
76 | } |
||
77 | |||
78 | $whereValues[] = $this->entityManager->getConnection()->quoteIdentifier($uniqueKey) . ' = ' . $this->entityManager->getConnection()->quote($value); |
||
79 | } |
||
80 | |||
81 | $sql[] = sprintf('UPDATE %s SET %s WHERE %s', $tableName, implode(',', $updateValues), implode(' AND ', $whereValues)); |
||
82 | } |
||
83 | |||
84 | return $sql; |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * Build an sql insert into query by the paramters provided |
||
89 | * |
||
90 | * @param ORM\Entity $entities Result array with all entities to create an insert for |
||
91 | * @param string $entityClassName Class of the specified entity (same as entities) |
||
92 | * @param array $ignoreFields fields not to use in the insert query |
||
93 | * |
||
94 | * @return string an insert sql query, of no result nul |
||
0 ignored issues
–
show
|
|||
95 | */ |
||
96 | public function buildInsertSql($entities, $entityClassName, $ignoreFields = array()) |
||
97 | { |
||
98 | if (count($entities) <= 0) { |
||
99 | return null; |
||
100 | } |
||
101 | |||
102 | $fieldNames = $this->entityManager->getClassMetadata($entityClassName)->getFieldNames(); |
||
103 | |||
104 | $tableName = $this->entityManager->getClassMetadata($entityClassName)->getTableName(); |
||
105 | $tableName = $this->entityManager->getConnection()->quoteIdentifier($tableName); |
||
106 | $fieldNames = array_diff($fieldNames, $ignoreFields); |
||
107 | |||
108 | $values = array(); |
||
109 | |||
110 | foreach ($entities as $entity) { |
||
111 | $insertValues = array(); |
||
112 | |||
113 | foreach ($fieldNames as $fieldName) { |
||
114 | $value = $entity->{'get'.$fieldName}(); |
||
115 | |||
116 | if ($value instanceof \DateTime) { |
||
117 | $value = $value->format('Y-m-d H:i:s'); |
||
118 | } |
||
119 | |||
120 | $insertValues[] = $this->entityManager->getConnection()->quote($value); |
||
121 | } |
||
122 | |||
123 | $values[] = '(' . implode(',', $insertValues) . ')'; |
||
124 | } |
||
125 | |||
126 | foreach ($fieldNames as $key => $fieldName) { |
||
127 | $columnName = $this->entityManager->getClassMetadata($entityClassName)->getColumnName($fieldName); |
||
128 | $fieldNames[$key] = $this->entityManager->getConnection()->quoteIdentifier($columnName); |
||
129 | } |
||
130 | |||
131 | $sql = sprintf('INSERT INTO %s (%s) VALUES %s', $tableName, implode(',', $fieldNames), implode(', ', $values)); |
||
132 | |||
133 | return $sql; |
||
134 | } |
||
135 | |||
136 | /** |
||
137 | * Get the sql query for all new translations added |
||
138 | * |
||
139 | * @return string sql query |
||
0 ignored issues
–
show
|
|||
140 | */ |
||
141 | public function getNewTranslationSql() |
||
142 | { |
||
143 | $translations = $this->translationRepository->findBy(array('flag' => \Kunstmaan\TranslatorBundle\Entity\Translation::FLAG_NEW)); |
||
144 | |||
145 | return $this->buildInsertSql($translations, '\Kunstmaan\TranslatorBundle\Entity\Translation', array('flag', 'id')); |
||
0 ignored issues
–
show
$translations is of type array , but the function expects a object<Kunstmaan\Transla...\Migrations\ORM\Entity> .
It seems like the type of the argument is not accepted by the function/method which you are calling. In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug. We suggest to add an explicit type cast like in the following example: function acceptsInteger($int) { }
$x = '123'; // string "123"
// Instead of
acceptsInteger($x);
// we recommend to use
acceptsInteger((integer) $x);
Loading history...
|
|||
146 | } |
||
147 | |||
148 | public function setTranslationRepository($translationRepository) |
||
149 | { |
||
150 | $this->translationRepository = $translationRepository; |
||
151 | } |
||
152 | |||
153 | public function setEntityManager($entityManager) |
||
154 | { |
||
155 | $this->entityManager = $entityManager; |
||
156 | } |
||
157 | } |
||
158 |
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.