Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / statistics / src / Plugin / migrate / destination / NodeCounter.php
1 <?php
2
3 namespace Drupal\statistics\Plugin\migrate\destination;
4
5 use Drupal\Core\Database\Connection;
6 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
7 use Drupal\migrate\Plugin\migrate\destination\DestinationBase;
8 use Drupal\migrate\Plugin\MigrationInterface;
9 use Drupal\migrate\Row;
10 use Symfony\Component\DependencyInjection\ContainerInterface;
11
12 /**
13  * Destination for node counter.
14  *
15  * @MigrateDestination(
16  *   id = "node_counter",
17  *   destination_module = "statistics"
18  * )
19  */
20 class NodeCounter extends DestinationBase implements ContainerFactoryPluginInterface {
21
22   /**
23    * The database connection.
24    *
25    * @var \Drupal\Core\Database\Connection
26    */
27   protected $connection;
28
29   /**
30    * Constructs a node counter plugin.
31    *
32    * @param array $configuration
33    *   Plugin configuration.
34    * @param string $plugin_id
35    *   The plugin ID.
36    * @param mixed $plugin_definition
37    *   The plugin definition.
38    * @param \Drupal\migrate\Plugin\MigrationInterface $migration
39    *   The current migration.
40    * @param \Drupal\Core\Database\Connection $connection
41    *   The database connection.
42    */
43   public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, Connection $connection) {
44     parent::__construct($configuration, $plugin_id, $plugin_definition, $migration);
45     $this->connection = $connection;
46   }
47
48   /**
49    * {@inheritdoc}
50    */
51   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
52     return new static(
53       $configuration,
54       $plugin_id,
55       $plugin_definition,
56       $migration,
57       $container->get('database')
58     );
59   }
60
61   /**
62    * {@inheritdoc}
63    */
64   public function getIds() {
65     return ['nid' => ['type' => 'integer']];
66   }
67
68   /**
69    * {@inheritdoc}
70    */
71   public function fields(MigrationInterface $migration = NULL) {
72     return [
73       'nid' => $this->t('The ID of the node to which these statistics apply.'),
74       'totalcount' => $this->t('The total number of times the node has been viewed.'),
75       'daycount' => $this->t('The total number of times the node has been viewed today.'),
76       'timestamp' => $this->t('The most recent time the node has been viewed.'),
77     ];
78   }
79
80   /**
81    * {@inheritdoc}
82    */
83   public function import(Row $row, array $old_destination_id_values = []) {
84     $nid = $row->getDestinationProperty('nid');
85     $daycount = $row->getDestinationProperty('daycount');
86     $totalcount = $row->getDestinationProperty('totalcount');
87     $timestamp = $row->getDestinationProperty('timestamp');
88
89     $this->connection
90       ->merge('node_counter')
91       ->key('nid', $nid)
92       ->fields([
93         'daycount' => $daycount,
94         'totalcount' => $totalcount,
95         'timestamp' => $timestamp,
96       ])
97       ->expression('daycount', 'daycount + :daycount', [':daycount' => $daycount])
98       ->expression('totalcount', 'totalcount + :totalcount', [':totalcount' => $totalcount])
99       ->expression('timestamp', 'CASE WHEN timestamp > :timestamp THEN timestamp ELSE :timestamp END', [':timestamp' => $timestamp])
100       ->execute();
101
102     return [$row->getDestinationProperty('nid')];
103   }
104
105 }