FileOptionBuilder   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 30
dl 0
loc 116
rs 10
c 2
b 0
f 0
wmc 12

7 Methods

Rating   Name   Duplication   Size   Complexity  
A isValidAndNotNull() 0 3 2
A properties() 0 9 1
A expirationTime() 0 11 2
A validated() 0 8 3
A build() 0 3 1
A dir() 0 4 1
A flushAfter() 0 11 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Silviooosilva\CacheerPhp\CacheStore\CacheManager\OptionBuilders;
6
7
use Silviooosilva\CacheerPhp\Support\TimeBuilder;
8
9
/**
10
 * Class FileOptionBuilder
11
 * @author Sílvio Silva <https://github.com/silviooosilva>
12
 * @package Silviooosilva\CacheerPhp
13
 */
14
class FileOptionBuilder
15
{
16
  /** @param null|string $cacheDir */
17
  private ?string $cacheDir = null;
18
19
  /** @param null|string $expirationTime */
20
  private ?string $expirationTime = null;
21
22
  /** @param null|string $flushAfter */
23
  private ?string $flushAfter = null;
24
25
  /** @param array $options */
26
  private array $options = [];
27
28
  /**
29
  * Directory where cache files will be stored.
30
  *
31
  * @param string $cacheDir
32
  * @return $this
33
  */
34
  public function dir(string $cacheDir)
35
  {
36
    $this->cacheDir = $cacheDir;
37
    return $this;
38
  }
39
40
  /**
41
  * Sets the expiration time for cache items.
42
  * @param ?string $expirationTime
43
  * @return $this|TimeBuilder
44
  */
45
  public function expirationTime(?string $expirationTime = null)
46
  {
47
48
    if (!is_null($expirationTime)) {
49
      $this->expirationTime = $expirationTime;
50
      return $this;
51
    }
52
53
    return new TimeBuilder(function ($formattedTime){
54
      $this->expirationTime = $formattedTime;
55
    }, $this);
56
  }
57
58
  /**
59
  * Sets the flush time for cache items.
60
  * This is the time after which the cache will be flushed.
61
  *
62
  * @param ?string $flushAfter
63
  * @return $this|TimeBuilder
64
  */
65
  public function flushAfter(?string $flushAfter = null)
66
  {
67
68
    if (!is_null($flushAfter)) {
69
      $this->flushAfter = mb_strtolower($flushAfter, 'UTF-8');
70
      return $this;
71
    }
72
73
    return new TimeBuilder(function ($formattedTime){
74
      $this->flushAfter = $formattedTime;
75
    }, $this);
76
  }
77
78
  /**
79
  * Builds the options array for file cache configuration.
80
  * @return array
81
  */
82
  public function build()
83
  {
84
    return $this->validated();
85
  }
86
87
  /**
88
  * Validates the properties and returns an array of options.
89
  * It checks if each property is valid and not null, then adds it to the options
90
  *
91
  * @return array
92
  */
93
  private function validated()
94
  {
95
    foreach ($this->properties() as $key => $value) {
96
        if ($this->isValidAndNotNull($value)) {
97
            $this->options[$key] = $value;
98
        }
99
    }
100
    return $this->options;
101
  }
102
103
  /**
104
  * Checks if the provided data is valid and not null.
105
  * This is used to ensure that only valid options are included in the final configuration.
106
  *
107
  * @param mixed $data
108
  * @return bool
109
  */
110
  private function isValidAndNotNull(mixed $data)
111
  {
112
    return !empty($data) ? true : false;
113
  }
114
115
  /**
116
  * Returns the properties of the FileOptionBuilder instance.
117
  * This method is used to gather the current state of the instance's properties.
118
  *
119
  * @return array
120
  */
121
  private function properties()
122
  {
123
    $properties = [
124
      'cacheDir' => $this->cacheDir,
125
      'expirationTime' => $this->expirationTime,
126
      'flushAfter' => $this->flushAfter
127
    ];
128
129
    return $properties;
130
  }
131
}
132