Passed
Push — main ( bce8dd...f4f8ca )
by Sílvio
56s
created

RedisOptionBuilder::flushAfter()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 10
rs 10
c 1
b 0
f 0
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 RedisOptionBuilder
11
 * @author Sílvio Silva <https://github.com/silviooosilva>
12
 * @package Silviooosilva\CacheerPhp
13
 */
14
class RedisOptionBuilder
15
{
16
  /** @var ?string */
17
  private ?string $namespace = null;
18
19
  /** @var ?string */
20
  private ?string $expirationTime = null;
21
22
  /** @var ?string */
23
  private ?string $flushAfter = null;
24
25
  /** @var array */
26
  private array $options = [];
27
28
  /**
29
   * Sets the Redis key namespace prefix.
30
   *
31
   * @param string $namespace
32
   * @return $this
33
   */
34
  public function setNamespace(string $namespace): self
35
  {
36
    $this->namespace = $namespace;
37
    return $this;
38
  }
39
40
  /**
41
   * Sets the default expiration time for keys.
42
   *
43
   * @param ?string $expirationTime
44
   * @return $this|TimeBuilder
45
   */
46
  public function expirationTime(?string $expirationTime = null)
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 auto-flush interval.
60
   *
61
   * @param ?string $flushAfter
62
   * @return $this|TimeBuilder
63
   */
64
  public function flushAfter(?string $flushAfter = null)
65
  {
66
    if (!is_null($flushAfter)) {
67
      $this->flushAfter = mb_strtolower($flushAfter, 'UTF-8');
68
      return $this;
69
    }
70
71
    return new TimeBuilder(function ($formattedTime) {
72
      $this->flushAfter = $formattedTime;
73
    }, $this);
74
  }
75
76
  /**
77
   * Builds the options array.
78
   *
79
   * @return array
80
   */
81
  public function build(): array
82
  {
83
    return $this->validated();
84
  }
85
86
  /**
87
   * Validate and assemble options.
88
   * @return array
89
   */
90
  private function validated(): array
91
  {
92
    foreach ($this->properties() as $key => $value) {
93
      if (!empty($value)) {
94
        $this->options[$key] = $value;
95
      }
96
    }
97
    return $this->options;
98
  }
99
100
  /**
101
   * Returns current properties.
102
   * @return array
103
   */
104
  private function properties(): array
105
  {
106
    return [
107
      'namespace'      => $this->namespace,
108
      'expirationTime' => $this->expirationTime,
109
      'flushAfter'     => $this->flushAfter,
110
    ];
111
  }
112
}
113
114