Yaffs site version 1.1
[yaffs-website] / web / modules / contrib / advagg / src / State / State.php
1 <?php
2
3 namespace Drupal\advagg\State;
4
5 use Drupal\Core\State\State as CoreState;
6
7 /**
8  * Provides AdvAgg State interfaces with a few extra commands.
9  */
10 abstract class State extends CoreState {
11
12   /**
13    * If the array isn't keyed by filepath the column the filepath is stored in.
14    */
15   protected $pathColumn = NULL;
16
17   /**
18    * Gets all stored information from this Key Value Store.
19    *
20    * @return array
21    *   An array of all key value pairs.
22    */
23   public function getAll() {
24     $values = $this->keyValueStore->getAll();
25     return $values;
26   }
27
28   /**
29    * Delete all stored information from this Key Value Store.
30    */
31   public function deleteAll() {
32     $this->keyValueStore->deleteAll();
33   }
34
35   /**
36    * Get a semi-random (randomness not guaranteed) key.
37    */
38   public function getRandomKey() {
39     $key = array_rand($this->getAll());
40     return $key;
41   }
42
43   /**
44    * Get a semi-random (randomness not guaranteed) value.
45    */
46   public function getRandom() {
47     return $this->get($this->getRandomKey());
48   }
49
50   /**
51    * Scan the filesystem for missing files and removee from database.
52    */
53   public function clearMissingFiles() {
54     $removed = [];
55     $values = $this->getAll();
56     if (empty($values)) {
57       return $removed;
58     }
59     if ($this->pathColumn) {
60       $values = array_column($values, NULL, $this->pathColumn);
61     }
62     foreach ($values as $path => $details) {
63       if (!file_exists($path)) {
64         $removed[$path] = $values[$path];
65         $this->delete($path);
66       }
67     }
68     return $removed;
69   }
70
71 }