Passed
Push — master ( d470e2...395826 )
by Christoph
28:10 queued 13:23
created
lib/private/SystemTag/SystemTagObjectMapper.php 1 patch
Indentation   +231 added lines, -231 removed lines patch added patch discarded remove patch
@@ -39,235 +39,235 @@
 block discarded – undo
39 39
 use Symfony\Component\EventDispatcher\EventDispatcherInterface;
40 40
 
41 41
 class SystemTagObjectMapper implements ISystemTagObjectMapper {
42
-	public const RELATION_TABLE = 'systemtag_object_mapping';
43
-
44
-	/** @var ISystemTagManager */
45
-	protected $tagManager;
46
-
47
-	/** @var IDBConnection */
48
-	protected $connection;
49
-
50
-	/** @var EventDispatcherInterface */
51
-	protected $dispatcher;
52
-
53
-	/**
54
-	 * Constructor.
55
-	 *
56
-	 * @param IDBConnection $connection database connection
57
-	 * @param ISystemTagManager $tagManager system tag manager
58
-	 * @param EventDispatcherInterface $dispatcher
59
-	 */
60
-	public function __construct(IDBConnection $connection, ISystemTagManager $tagManager, EventDispatcherInterface $dispatcher) {
61
-		$this->connection = $connection;
62
-		$this->tagManager = $tagManager;
63
-		$this->dispatcher = $dispatcher;
64
-	}
65
-
66
-	/**
67
-	 * {@inheritdoc}
68
-	 */
69
-	public function getTagIdsForObjects($objIds, string $objectType): array {
70
-		if (!\is_array($objIds)) {
71
-			$objIds = [$objIds];
72
-		} elseif (empty($objIds)) {
73
-			return [];
74
-		}
75
-
76
-		$query = $this->connection->getQueryBuilder();
77
-		$query->select(['systemtagid', 'objectid'])
78
-			->from(self::RELATION_TABLE)
79
-			->where($query->expr()->in('objectid', $query->createParameter('objectids')))
80
-			->andWhere($query->expr()->eq('objecttype', $query->createParameter('objecttype')))
81
-			->setParameter('objectids', $objIds, IQueryBuilder::PARAM_STR_ARRAY)
82
-			->setParameter('objecttype', $objectType)
83
-			->addOrderBy('objectid', 'ASC')
84
-			->addOrderBy('systemtagid', 'ASC');
85
-
86
-		$mapping = [];
87
-		foreach ($objIds as $objId) {
88
-			$mapping[$objId] = [];
89
-		}
90
-
91
-		$result = $query->execute();
92
-		while ($row = $result->fetch()) {
93
-			$objectId = $row['objectid'];
94
-			$mapping[$objectId][] = $row['systemtagid'];
95
-		}
96
-
97
-		$result->closeCursor();
98
-
99
-		return $mapping;
100
-	}
101
-
102
-	/**
103
-	 * {@inheritdoc}
104
-	 */
105
-	public function getObjectIdsForTags($tagIds, string $objectType, int $limit = 0, string $offset = ''): array {
106
-		if (!\is_array($tagIds)) {
107
-			$tagIds = [$tagIds];
108
-		}
109
-
110
-		$this->assertTagsExist($tagIds);
111
-
112
-		$query = $this->connection->getQueryBuilder();
113
-		$query->selectDistinct('objectid')
114
-			->from(self::RELATION_TABLE)
115
-			->where($query->expr()->in('systemtagid', $query->createNamedParameter($tagIds, IQueryBuilder::PARAM_INT_ARRAY)))
116
-			->andWhere($query->expr()->eq('objecttype', $query->createNamedParameter($objectType)));
117
-
118
-		if ($limit) {
119
-			if (\count($tagIds) !== 1) {
120
-				throw new \InvalidArgumentException('Limit is only allowed with a single tag');
121
-			}
122
-
123
-			$query->setMaxResults($limit)
124
-				->orderBy('objectid', 'ASC');
125
-
126
-			if ($offset !== '') {
127
-				$query->andWhere($query->expr()->gt('objectid', $query->createNamedParameter($offset)));
128
-			}
129
-		}
130
-
131
-		$objectIds = [];
132
-
133
-		$result = $query->execute();
134
-		while ($row = $result->fetch()) {
135
-			$objectIds[] = $row['objectid'];
136
-		}
137
-
138
-		return $objectIds;
139
-	}
140
-
141
-	/**
142
-	 * {@inheritdoc}
143
-	 */
144
-	public function assignTags(string $objId, string $objectType, $tagIds) {
145
-		if (!\is_array($tagIds)) {
146
-			$tagIds = [$tagIds];
147
-		}
148
-
149
-		$this->assertTagsExist($tagIds);
150
-
151
-		$query = $this->connection->getQueryBuilder();
152
-		$query->insert(self::RELATION_TABLE)
153
-			->values([
154
-				'objectid' => $query->createNamedParameter($objId),
155
-				'objecttype' => $query->createNamedParameter($objectType),
156
-				'systemtagid' => $query->createParameter('tagid'),
157
-			]);
158
-
159
-		$tagsAssigned = [];
160
-		foreach ($tagIds as $tagId) {
161
-			try {
162
-				$query->setParameter('tagid', $tagId);
163
-				$query->execute();
164
-				$tagsAssigned[] = $tagId;
165
-			} catch (UniqueConstraintViolationException $e) {
166
-				// ignore existing relations
167
-			}
168
-		}
169
-
170
-		if (empty($tagsAssigned)) {
171
-			return;
172
-		}
173
-
174
-		$this->dispatcher->dispatch(MapperEvent::EVENT_ASSIGN, new MapperEvent(
175
-			MapperEvent::EVENT_ASSIGN,
176
-			$objectType,
177
-			$objId,
178
-			$tagsAssigned
179
-		));
180
-	}
181
-
182
-	/**
183
-	 * {@inheritdoc}
184
-	 */
185
-	public function unassignTags(string $objId, string $objectType, $tagIds) {
186
-		if (!\is_array($tagIds)) {
187
-			$tagIds = [$tagIds];
188
-		}
189
-
190
-		$this->assertTagsExist($tagIds);
191
-
192
-		$query = $this->connection->getQueryBuilder();
193
-		$query->delete(self::RELATION_TABLE)
194
-			->where($query->expr()->eq('objectid', $query->createParameter('objectid')))
195
-			->andWhere($query->expr()->eq('objecttype', $query->createParameter('objecttype')))
196
-			->andWhere($query->expr()->in('systemtagid', $query->createParameter('tagids')))
197
-			->setParameter('objectid', $objId)
198
-			->setParameter('objecttype', $objectType)
199
-			->setParameter('tagids', $tagIds, IQueryBuilder::PARAM_INT_ARRAY)
200
-			->execute();
201
-
202
-		$this->dispatcher->dispatch(MapperEvent::EVENT_UNASSIGN, new MapperEvent(
203
-			MapperEvent::EVENT_UNASSIGN,
204
-			$objectType,
205
-			$objId,
206
-			$tagIds
207
-		));
208
-	}
209
-
210
-	/**
211
-	 * {@inheritdoc}
212
-	 */
213
-	public function haveTag($objIds, string $objectType, string $tagId, bool $all = true): bool {
214
-		$this->assertTagsExist([$tagId]);
215
-
216
-		if (!\is_array($objIds)) {
217
-			$objIds = [$objIds];
218
-		}
219
-
220
-		$query = $this->connection->getQueryBuilder();
221
-
222
-		if (!$all) {
223
-			// If we only need one entry, we make the query lighter, by not
224
-			// counting the elements
225
-			$query->select('*')
226
-				->setMaxResults(1);
227
-		} else {
228
-			$query->select($query->func()->count($query->expr()->literal(1)));
229
-		}
230
-
231
-		$query->from(self::RELATION_TABLE)
232
-			->where($query->expr()->in('objectid', $query->createParameter('objectids')))
233
-			->andWhere($query->expr()->eq('objecttype', $query->createParameter('objecttype')))
234
-			->andWhere($query->expr()->eq('systemtagid', $query->createParameter('tagid')))
235
-			->setParameter('objectids', $objIds, IQueryBuilder::PARAM_STR_ARRAY)
236
-			->setParameter('tagid', $tagId)
237
-			->setParameter('objecttype', $objectType);
238
-
239
-		$result = $query->execute();
240
-		$row = $result->fetch(\PDO::FETCH_NUM);
241
-		$result->closeCursor();
242
-
243
-		if ($all) {
244
-			return ((int)$row[0] === \count($objIds));
245
-		}
246
-
247
-		return (bool) $row;
248
-	}
249
-
250
-	/**
251
-	 * Asserts that all the given tag ids exist.
252
-	 *
253
-	 * @param string[] $tagIds tag ids to check
254
-	 *
255
-	 * @throws \OCP\SystemTag\TagNotFoundException if at least one tag did not exist
256
-	 */
257
-	private function assertTagsExist($tagIds) {
258
-		$tags = $this->tagManager->getTagsByIds($tagIds);
259
-		if (\count($tags) !== \count($tagIds)) {
260
-			// at least one tag missing, bail out
261
-			$foundTagIds = array_map(
262
-				function (ISystemTag $tag) {
263
-					return $tag->getId();
264
-				},
265
-				$tags
266
-			);
267
-			$missingTagIds = array_diff($tagIds, $foundTagIds);
268
-			throw new TagNotFoundException(
269
-				'Tags not found', 0, null, $missingTagIds
270
-			);
271
-		}
272
-	}
42
+    public const RELATION_TABLE = 'systemtag_object_mapping';
43
+
44
+    /** @var ISystemTagManager */
45
+    protected $tagManager;
46
+
47
+    /** @var IDBConnection */
48
+    protected $connection;
49
+
50
+    /** @var EventDispatcherInterface */
51
+    protected $dispatcher;
52
+
53
+    /**
54
+     * Constructor.
55
+     *
56
+     * @param IDBConnection $connection database connection
57
+     * @param ISystemTagManager $tagManager system tag manager
58
+     * @param EventDispatcherInterface $dispatcher
59
+     */
60
+    public function __construct(IDBConnection $connection, ISystemTagManager $tagManager, EventDispatcherInterface $dispatcher) {
61
+        $this->connection = $connection;
62
+        $this->tagManager = $tagManager;
63
+        $this->dispatcher = $dispatcher;
64
+    }
65
+
66
+    /**
67
+     * {@inheritdoc}
68
+     */
69
+    public function getTagIdsForObjects($objIds, string $objectType): array {
70
+        if (!\is_array($objIds)) {
71
+            $objIds = [$objIds];
72
+        } elseif (empty($objIds)) {
73
+            return [];
74
+        }
75
+
76
+        $query = $this->connection->getQueryBuilder();
77
+        $query->select(['systemtagid', 'objectid'])
78
+            ->from(self::RELATION_TABLE)
79
+            ->where($query->expr()->in('objectid', $query->createParameter('objectids')))
80
+            ->andWhere($query->expr()->eq('objecttype', $query->createParameter('objecttype')))
81
+            ->setParameter('objectids', $objIds, IQueryBuilder::PARAM_STR_ARRAY)
82
+            ->setParameter('objecttype', $objectType)
83
+            ->addOrderBy('objectid', 'ASC')
84
+            ->addOrderBy('systemtagid', 'ASC');
85
+
86
+        $mapping = [];
87
+        foreach ($objIds as $objId) {
88
+            $mapping[$objId] = [];
89
+        }
90
+
91
+        $result = $query->execute();
92
+        while ($row = $result->fetch()) {
93
+            $objectId = $row['objectid'];
94
+            $mapping[$objectId][] = $row['systemtagid'];
95
+        }
96
+
97
+        $result->closeCursor();
98
+
99
+        return $mapping;
100
+    }
101
+
102
+    /**
103
+     * {@inheritdoc}
104
+     */
105
+    public function getObjectIdsForTags($tagIds, string $objectType, int $limit = 0, string $offset = ''): array {
106
+        if (!\is_array($tagIds)) {
107
+            $tagIds = [$tagIds];
108
+        }
109
+
110
+        $this->assertTagsExist($tagIds);
111
+
112
+        $query = $this->connection->getQueryBuilder();
113
+        $query->selectDistinct('objectid')
114
+            ->from(self::RELATION_TABLE)
115
+            ->where($query->expr()->in('systemtagid', $query->createNamedParameter($tagIds, IQueryBuilder::PARAM_INT_ARRAY)))
116
+            ->andWhere($query->expr()->eq('objecttype', $query->createNamedParameter($objectType)));
117
+
118
+        if ($limit) {
119
+            if (\count($tagIds) !== 1) {
120
+                throw new \InvalidArgumentException('Limit is only allowed with a single tag');
121
+            }
122
+
123
+            $query->setMaxResults($limit)
124
+                ->orderBy('objectid', 'ASC');
125
+
126
+            if ($offset !== '') {
127
+                $query->andWhere($query->expr()->gt('objectid', $query->createNamedParameter($offset)));
128
+            }
129
+        }
130
+
131
+        $objectIds = [];
132
+
133
+        $result = $query->execute();
134
+        while ($row = $result->fetch()) {
135
+            $objectIds[] = $row['objectid'];
136
+        }
137
+
138
+        return $objectIds;
139
+    }
140
+
141
+    /**
142
+     * {@inheritdoc}
143
+     */
144
+    public function assignTags(string $objId, string $objectType, $tagIds) {
145
+        if (!\is_array($tagIds)) {
146
+            $tagIds = [$tagIds];
147
+        }
148
+
149
+        $this->assertTagsExist($tagIds);
150
+
151
+        $query = $this->connection->getQueryBuilder();
152
+        $query->insert(self::RELATION_TABLE)
153
+            ->values([
154
+                'objectid' => $query->createNamedParameter($objId),
155
+                'objecttype' => $query->createNamedParameter($objectType),
156
+                'systemtagid' => $query->createParameter('tagid'),
157
+            ]);
158
+
159
+        $tagsAssigned = [];
160
+        foreach ($tagIds as $tagId) {
161
+            try {
162
+                $query->setParameter('tagid', $tagId);
163
+                $query->execute();
164
+                $tagsAssigned[] = $tagId;
165
+            } catch (UniqueConstraintViolationException $e) {
166
+                // ignore existing relations
167
+            }
168
+        }
169
+
170
+        if (empty($tagsAssigned)) {
171
+            return;
172
+        }
173
+
174
+        $this->dispatcher->dispatch(MapperEvent::EVENT_ASSIGN, new MapperEvent(
175
+            MapperEvent::EVENT_ASSIGN,
176
+            $objectType,
177
+            $objId,
178
+            $tagsAssigned
179
+        ));
180
+    }
181
+
182
+    /**
183
+     * {@inheritdoc}
184
+     */
185
+    public function unassignTags(string $objId, string $objectType, $tagIds) {
186
+        if (!\is_array($tagIds)) {
187
+            $tagIds = [$tagIds];
188
+        }
189
+
190
+        $this->assertTagsExist($tagIds);
191
+
192
+        $query = $this->connection->getQueryBuilder();
193
+        $query->delete(self::RELATION_TABLE)
194
+            ->where($query->expr()->eq('objectid', $query->createParameter('objectid')))
195
+            ->andWhere($query->expr()->eq('objecttype', $query->createParameter('objecttype')))
196
+            ->andWhere($query->expr()->in('systemtagid', $query->createParameter('tagids')))
197
+            ->setParameter('objectid', $objId)
198
+            ->setParameter('objecttype', $objectType)
199
+            ->setParameter('tagids', $tagIds, IQueryBuilder::PARAM_INT_ARRAY)
200
+            ->execute();
201
+
202
+        $this->dispatcher->dispatch(MapperEvent::EVENT_UNASSIGN, new MapperEvent(
203
+            MapperEvent::EVENT_UNASSIGN,
204
+            $objectType,
205
+            $objId,
206
+            $tagIds
207
+        ));
208
+    }
209
+
210
+    /**
211
+     * {@inheritdoc}
212
+     */
213
+    public function haveTag($objIds, string $objectType, string $tagId, bool $all = true): bool {
214
+        $this->assertTagsExist([$tagId]);
215
+
216
+        if (!\is_array($objIds)) {
217
+            $objIds = [$objIds];
218
+        }
219
+
220
+        $query = $this->connection->getQueryBuilder();
221
+
222
+        if (!$all) {
223
+            // If we only need one entry, we make the query lighter, by not
224
+            // counting the elements
225
+            $query->select('*')
226
+                ->setMaxResults(1);
227
+        } else {
228
+            $query->select($query->func()->count($query->expr()->literal(1)));
229
+        }
230
+
231
+        $query->from(self::RELATION_TABLE)
232
+            ->where($query->expr()->in('objectid', $query->createParameter('objectids')))
233
+            ->andWhere($query->expr()->eq('objecttype', $query->createParameter('objecttype')))
234
+            ->andWhere($query->expr()->eq('systemtagid', $query->createParameter('tagid')))
235
+            ->setParameter('objectids', $objIds, IQueryBuilder::PARAM_STR_ARRAY)
236
+            ->setParameter('tagid', $tagId)
237
+            ->setParameter('objecttype', $objectType);
238
+
239
+        $result = $query->execute();
240
+        $row = $result->fetch(\PDO::FETCH_NUM);
241
+        $result->closeCursor();
242
+
243
+        if ($all) {
244
+            return ((int)$row[0] === \count($objIds));
245
+        }
246
+
247
+        return (bool) $row;
248
+    }
249
+
250
+    /**
251
+     * Asserts that all the given tag ids exist.
252
+     *
253
+     * @param string[] $tagIds tag ids to check
254
+     *
255
+     * @throws \OCP\SystemTag\TagNotFoundException if at least one tag did not exist
256
+     */
257
+    private function assertTagsExist($tagIds) {
258
+        $tags = $this->tagManager->getTagsByIds($tagIds);
259
+        if (\count($tags) !== \count($tagIds)) {
260
+            // at least one tag missing, bail out
261
+            $foundTagIds = array_map(
262
+                function (ISystemTag $tag) {
263
+                    return $tag->getId();
264
+                },
265
+                $tags
266
+            );
267
+            $missingTagIds = array_diff($tagIds, $foundTagIds);
268
+            throw new TagNotFoundException(
269
+                'Tags not found', 0, null, $missingTagIds
270
+            );
271
+        }
272
+    }
273 273
 }
Please login to merge, or discard this patch.