1: <?php
2: /**
3: * Chippyash Digest Authentication Manager
4: *
5: * @copyright Ashley Kitson, UK, 2014
6: * @license GPL 3.0+
7: */
8: namespace Chippyash\Authentication\Manager\Digest;
9:
10: use Chippyash\Authentication\Manager\Digest\DigestCollectionInterface;
11: use Chippyash\Authentication\Manager\Encoder\DigestEncoderInterface;
12: use Chippyash\Authentication\Manager\Exceptions\AuthManagerException;
13: use Chippyash\Type\String\StringType;
14: use Chippyash\Type\Number\IntType;
15: use Chippyash\Type\BoolType;
16:
17: /**
18: * A collection of Digests
19: */
20: abstract class AbstractDigestCollection implements DigestCollectionInterface, \Countable
21: {
22: const ERR_NO_DIGEST_TPL = 'No digest at index %d';
23:
24: /**
25: * File write options
26: * @see \file_put_contents
27: *
28: * @var int
29: */
30: protected $writeOptions = LOCK_EX;
31:
32: /**
33: * Name of file that digest collection is stored in
34: *
35: * @var StringType
36: */
37: protected $fileName;
38:
39: /**
40: * Digest encoder
41: * @var Chippyash\Authentication\Manager\Encoder\DigestEncoderInterface
42: */
43: protected $encoder;
44:
45: /**
46: * Collection of digest items
47: *
48: * @var array
49: */
50: protected $collection = [];
51:
52: public function __construct(StringType $fileName, array $digests = [])
53: {
54: $this->collection = $digests;
55: $this->fileName = $fileName;
56: }
57:
58: /**
59: * Set file writing options
60: * @see \file_put_contents
61: *
62: * @param IntType $options file_put_contents options
63: *
64: * @return Fluent Interface
65: */
66: public function setWriteOptions(IntType $options){
67: $this->writeOptions = $options();
68: return $this;
69: }
70:
71: /**
72: * Set the encoder
73: *
74: * @param DigestEncoderInterface $encoder
75: * @return Fluent Interface
76: */
77: public function setEncoder(DigestEncoderInterface $encoder)
78: {
79: $this->encoder = $encoder;
80:
81: return $this;
82: }
83:
84: /**
85: * @interface \Countable
86: *
87: * @return int
88: */
89: public function count()
90: {
91: return count($this->collection);
92: }
93:
94: /**
95: * Get digest item
96: *
97: * @param IntType $index
98: *
99: * @return array Digest item
100: *
101: * @throws Chippyash\Authentication\Manager\Exceptions\AuthManagerException
102: */
103: public function get(IntType $index)
104: {
105: if (!isset($this->collection[$index()])) {
106: throw new AuthManagerException(sprintf(self::ERR_NO_DIGEST_TPL, $index()));
107: }
108:
109: return $this->collection[$index()];
110: }
111:
112: /**
113: * Delete digest item
114: *
115: * @param IntType $index
116: *
117: * @return Chippyash\Type\BoolType true on success else false
118: */
119: public function del(IntType $index)
120: {
121: if (!isset($this->collection[$index()])) {
122: return new BoolType(false);
123: }
124:
125: unset($this->collection[$index()]);
126: $this->collection = array_values($this->collection);
127:
128: return new BoolType(true);
129: }
130:
131: /**
132: * Return index into collection for a digest given its uid
133: *
134: * @param StringType $uid user id
135: *
136: * @return Chippyash\Type\Number\IntType|false
137: */
138: abstract public function findByUid(StringType $uid);
139:
140: /**
141: * Read the digest into the collection from file
142: *
143: * @return Chippyash\Type\BoolType true on success else false
144: */
145: abstract public function read();
146:
147: /**
148: * Write the collection to file
149: *
150: * @return Chippyash\Type\BoolType true on success else false
151: */
152: abstract public function write();
153:
154: /**
155: * Add a digest line to the collection
156: *
157: * @param StringType $uid user id
158: * @param StringType $pwd password
159: *
160: * @return Chippyash\Type\BoolType true on success else false
161: */
162: abstract public function add(StringType $uid, StringType $pwd);
163:
164: /**
165: * Return the collection item as a raw digest string
166: *
167: * @param IntType $index Index into collection
168: *
169: * @return StringType
170: */
171: abstract public function asString(IntType $index);
172: }
173: