Updated all the contrib modules to their latest versions.
[yaffs-website] / web / modules / contrib / eu_cookie_compliance / src / Plugin / ConsentStorageBase.php
1 <?php
2
3 namespace Drupal\eu_cookie_compliance\Plugin;
4
5 use Drupal\Core\Plugin\PluginBase;
6 use Symfony\Component\DependencyInjection\ContainerInterface;
7 use Drupal\Core\Config\ConfigFactoryInterface;
8
9 /**
10  * Provides a base class for a consent storage.
11  *
12  * @see \Drupal\eu_cookie_compliance\Plugin\ConsentStorageInterface
13  * @see \Drupal\eu_cookie_compliance\Plugin\ConsentStorageManager
14  * @see \Drupal\eu_cookie_compliance\Plugin\ConsentStorageManagerInterface
15  * @see plugin_api
16  */
17 abstract class ConsentStorageBase extends PluginBase implements ConsentStorageInterface {
18
19   /**
20    * The config factory.
21    *
22    * Subclasses should use the self::config() method, which may be overridden to
23    * address specific needs when loading config, rather than this property
24    * directly. See \Drupal\Core\Form\ConfigFormBase::config() for an example of
25    * this.
26    *
27    * @var \Drupal\Core\Config\ConfigFactoryInterface
28    */
29   protected $configFactory;
30
31   /**
32    * Constructs a ConsentStorageBase object.
33    *
34    * @param array $configuration
35    *   A configuration array containing information about the plugin instance.
36    * @param string $plugin_id
37    *   The plugin_id for the plugin instance.
38    * @param mixed $plugin_definition
39    *   The plugin implementation definition.
40    * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
41    *   The factory for configuration objects.
42    */
43   public function __construct(array $configuration, $plugin_id, $plugin_definition, ConfigFactoryInterface $config_factory) {
44     parent::__construct($configuration, $plugin_id, $plugin_definition);
45
46     $this->configFactory = $config_factory;
47   }
48
49   /**
50    * {@inheritdoc}
51    */
52   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
53     return new static(
54       $configuration,
55       $plugin_id,
56       $plugin_definition,
57       $container->get('config.factory')
58     );
59   }
60
61   /**
62    * {@inheritdoc}
63    */
64   public function label() {
65     return $this->pluginDefinition['label'];
66   }
67
68   /**
69    * {@inheritdoc}
70    */
71   public function description() {
72     return $this->pluginDefinition['description'];
73   }
74
75   /**
76    * {@inheritdoc}
77    */
78   public function getStatus() {
79     return TRUE;
80   }
81
82   /**
83    * Get the current revision of the privacy policy node.
84    *
85    * @return bool|int
86    *   Returns the latest revision ID of the curreny privacy policy node, or
87    *   FALSE if no priacy policy exists.
88    *
89    * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
90    */
91   public function getCurrentPolicyNodeRevision() {
92     $config = $this->configFactory->get('eu_cookie_compliance.settings');
93     $cookie_policy_link = $config->get('popup_link');
94     $cookie_policy_drupal_path = \Drupal::service('path.alias_manager')->getPathByAlias($cookie_policy_link, \Drupal::languageManager()->getCurrentLanguage()->getId());
95     if (substr($cookie_policy_drupal_path, 0, 6) == '/node/') {
96       $node_id = explode('/', $cookie_policy_drupal_path)[2];
97       /* @var \Drupal\node\Entity\Node $node */
98       $node = \Drupal::entityTypeManager()->getStorage('node')->load($node_id);
99       return $node->getRevisionId();
100     }
101     else {
102       return FALSE;
103     }
104   }
105
106   /**
107    * Register consent.
108    *
109    * In addition to the parameters passed to the function, consider storing the
110    * uid, ip address, timestamp and the current revision of the cookie policy.
111    *
112    * @param string $consent_type
113    *   "banner" if the consent is given to the cookie banner or the form ID when
114    *   consent is given on a form.
115    *
116    * @return bool
117    *   Returns TRUE when the consent has been stored successfully, FALSE on
118    *   error.
119    */
120   abstract protected function registerConsent($consent_type);
121
122 }