comparison core/modules/comment/src/CommentStatistics.php @ 5:12f9dff5fda9 tip

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:34:47 +0100
parents c75dbcec494b
children
comparison
equal deleted inserted replaced
4:a9cd425dd02b 5:12f9dff5fda9
1 <?php 1 <?php
2 2
3 namespace Drupal\comment; 3 namespace Drupal\comment;
4 4
5 use Drupal\Core\Database\Connection; 5 use Drupal\Core\Database\Connection;
6 use Drupal\Core\DependencyInjection\DeprecatedServicePropertyTrait;
7 use Drupal\Core\Entity\EntityTypeManagerInterface;
6 use Drupal\Core\Entity\FieldableEntityInterface; 8 use Drupal\Core\Entity\FieldableEntityInterface;
7 use Drupal\Core\Entity\EntityChangedInterface; 9 use Drupal\Core\Entity\EntityChangedInterface;
8 use Drupal\Core\Entity\EntityInterface; 10 use Drupal\Core\Entity\EntityInterface;
9 use Drupal\Core\Entity\EntityManagerInterface;
10 use Drupal\Core\State\StateInterface; 11 use Drupal\Core\State\StateInterface;
11 use Drupal\Core\Session\AccountInterface; 12 use Drupal\Core\Session\AccountInterface;
12 use Drupal\user\EntityOwnerInterface; 13 use Drupal\user\EntityOwnerInterface;
13 14
14 class CommentStatistics implements CommentStatisticsInterface { 15 class CommentStatistics implements CommentStatisticsInterface {
16 use DeprecatedServicePropertyTrait;
17
18 /**
19 * {@inheritdoc}
20 */
21 protected $deprecatedProperties = ['entityManager' => 'entity.manager'];
15 22
16 /** 23 /**
17 * The current database connection. 24 * The current database connection.
18 * 25 *
19 * @var \Drupal\Core\Database\Connection 26 * @var \Drupal\Core\Database\Connection
20 */ 27 */
21 protected $database; 28 protected $database;
22 29
23 /** 30 /**
31 * The replica database connection.
32 *
33 * @var \Drupal\Core\Database\Connection
34 */
35 protected $databaseReplica;
36
37 /**
24 * The current logged in user. 38 * The current logged in user.
25 * 39 *
26 * @var \Drupal\Core\Session\AccountInterface 40 * @var \Drupal\Core\Session\AccountInterface
27 */ 41 */
28 protected $currentUser; 42 protected $currentUser;
29 43
30 /** 44 /**
31 * The entity manager service. 45 * The entity type manager.
32 * 46 *
33 * @var \Drupal\Core\Entity\EntityManagerInterface 47 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
34 */ 48 */
35 protected $entityManager; 49 protected $entityTypeManager;
36 50
37 /** 51 /**
38 * The state service. 52 * The state service.
39 * 53 *
40 * @var \Drupal\Core\State\StateInterface 54 * @var \Drupal\Core\State\StateInterface
46 * 60 *
47 * @param \Drupal\Core\Database\Connection $database 61 * @param \Drupal\Core\Database\Connection $database
48 * The active database connection. 62 * The active database connection.
49 * @param \Drupal\Core\Session\AccountInterface $current_user 63 * @param \Drupal\Core\Session\AccountInterface $current_user
50 * The current logged in user. 64 * The current logged in user.
51 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager 65 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
52 * The entity manager service. 66 * The entity type manager.
53 * @param \Drupal\Core\State\StateInterface $state 67 * @param \Drupal\Core\State\StateInterface $state
54 * The state service. 68 * The state service.
55 */ 69 * @param \Drupal\Core\Database\Connection|null $database_replica
56 public function __construct(Connection $database, AccountInterface $current_user, EntityManagerInterface $entity_manager, StateInterface $state) { 70 * (Optional) the replica database connection.
71 */
72 public function __construct(Connection $database, AccountInterface $current_user, EntityTypeManagerInterface $entity_type_manager, StateInterface $state, Connection $database_replica = NULL) {
57 $this->database = $database; 73 $this->database = $database;
74 $this->databaseReplica = $database_replica ?: $database;
58 $this->currentUser = $current_user; 75 $this->currentUser = $current_user;
59 $this->entityManager = $entity_manager; 76 $this->entityTypeManager = $entity_type_manager;
60 $this->state = $state; 77 $this->state = $state;
61 } 78 }
62 79
63 /** 80 /**
64 * {@inheritdoc} 81 * {@inheritdoc}
65 */ 82 */
66 public function read($entities, $entity_type, $accurate = TRUE) { 83 public function read($entities, $entity_type, $accurate = TRUE) {
67 $options = $accurate ? [] : ['target' => 'replica']; 84 $connection = $accurate ? $this->database : $this->databaseReplica;
68 $stats = $this->database->select('comment_entity_statistics', 'ces', $options) 85 $stats = $connection->select('comment_entity_statistics', 'ces')
69 ->fields('ces') 86 ->fields('ces')
70 ->condition('ces.entity_id', array_keys($entities), 'IN') 87 ->condition('ces.entity_id', array_keys($entities), 'IN')
71 ->condition('ces.entity_type', $entity_type) 88 ->condition('ces.entity_type', $entity_type)
72 ->execute(); 89 ->execute();
73 90
252 ->execute(); 269 ->execute();
253 } 270 }
254 271
255 // Reset the cache of the commented entity so that when the entity is loaded 272 // Reset the cache of the commented entity so that when the entity is loaded
256 // the next time, the statistics will be loaded again. 273 // the next time, the statistics will be loaded again.
257 $this->entityManager->getStorage($comment->getCommentedEntityTypeId())->resetCache([$comment->getCommentedEntityId()]); 274 $this->entityTypeManager->getStorage($comment->getCommentedEntityTypeId())->resetCache([$comment->getCommentedEntityId()]);
258 } 275 }
259 276
260 } 277 }