Passed
Push — main ( b79ce7...ac617d )
by Sílvio
02:54
created

FileOptionBuilder::build()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Silviooosilva\CacheerPhp\CacheStore\CacheManager\OptionBuilders;
4
5
use Silviooosilva\CacheerPhp\Support\TimeBuilder;
6
7
/**
8
 * Class FileOptionBuilder
9
 * @author Sílvio Silva <https://github.com/silviooosilva>
10
 * @package Silviooosilva\CacheerPhp
11
 */
12
class FileOptionBuilder
13
{
14
  /** @param null|string $cacheDir */
15
  private ?string $cacheDir = null;
16
17
  /** @param null|string $expirationTime */
18
  private ?string $expirationTime = null;
19
20
  /** @param null|string $flushAfter */
21
  private ?string $flushAfter = null;
22
23
  /** @param array $options */
24
  private array $options = [];
25
26
  /**
27
  * @param string $cacheDir
28
  * @return $this
29
  */
30
  public function dir(string $cacheDir)
31
  {
32
    $this->cacheDir = $cacheDir;
33
    return $this;
34
  }
35
36
  /**
37
  * @param ?string $expirationTime
38
  * @return $this|TimeBuilder
39
  */
40
  public function expirationTime(?string $expirationTime = null)
41
  {
42
43
    if (!is_null($expirationTime)) {
44
      $this->expirationTime = $expirationTime;
45
      return $this;
46
    }
47
48
    return new TimeBuilder(function ($formattedTime){
49
      $this->expirationTime = $formattedTime;
50
    }, $this);
51
  }
52
53
  /**
54
  * @param ?string $flushAfter
55
  * @return $this|TimeBuilder
56
  */
57
  public function flushAfter(?string $flushAfter = null)
58
  {
59
60
    if (!is_null($flushAfter)) {
61
      $this->flushAfter = mb_strtolower($flushAfter, 'UTF-8');
62
      return $this;
63
    }
64
65
    return new TimeBuilder(function ($formattedTime){
66
      $this->flushAfter = $formattedTime;
67
    }, $this);
68
  }
69
70
  /**
71
  * @return array
72
  */
73
  public function build()
74
  {
75
    return $this->validated();
76
  }
77
78
  /**
79
  * @return array
80
  */
81
  private function validated()
82
  {
83
    foreach ($this->properties() as $key => $value) {
84
        if ($this->isValidAndNotNull($value)) {
85
            $this->options[$key] = $value;
86
        }
87
    }
88
    return $this->options;
89
  }
90
91
  /**
92
  * @param mixed $data
93
  * @return bool
94
  */
95
  private function isValidAndNotNull(mixed $data)
96
  {
97
    return !empty($data) ? true : false;
98
  }
99
100
  /**
101
  * @return array
102
  */
103
  private function properties()
104
  {
105
    $properties = [
106
      'cacheDir' => $this->cacheDir,
107
      'expirationTime' => $this->expirationTime,
108
      'flushAfter' => $this->flushAfter
109
    ];
110
111
    return $properties;
112
  }
113
}
114