Completed
Pull Request — master (#39)
by
unknown
02:33
created

File::create()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
/**
3
 * @author Threema GmbH
4
 * @copyright Copyright (c) 2015-2016 Threema GmbH
5
 */
6
7
namespace Threema\MsgApi\PublicKeyStores;
8
9
use Threema\Core\Exception;
10
use Threema\MsgApi\PublicKeyStore;
11
12
/**
13
 * Store the PublicKeys in a ascii file
14
 *
15
 * @package Threema\MsgApi\PublicKeyStores
16
 */
17
class File extends PublicKeyStore {
18
	/**
19
	 * @var string
20
	 */
21
	private $file;
22
23
	/**
24
	 * @param string $file Valid, read and writable file
25
	 * @throws Exception if the file does not exist or not writable
26
	 */
27
	public function __construct($file) {
28
		if(false === is_writable($file)) {
29
			throw new Exception('file '.$file.' does not exist or is not writable');
30
		}
31
		$this->file = $file;
32
	}
33
34
	/**
35
	 * return null if the public key not found in the store
36
	 * @param string $threemaId
37
	 * @return null|string
38
	 * @throws Exception
39
	 */
40
	function findPublicKey($threemaId) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
41
		$storeHandle = fopen($this->file, 'r');
42
		if(false === $storeHandle) {
43
			throw new Exception('could not open file '.$this->file);
44
		}
45
		else {
46
			$threemaId = strtoupper($threemaId);
47
			$publicKey = null;
48
			while (!feof($storeHandle)) {
49
				$buffer = fgets($storeHandle, 4096);
50
				if(substr($buffer, 0, 8) == $threemaId) {
51
					$publicKey = str_replace("\n", '', substr($buffer, 8));
52
					continue;
53
				}
54
				// Process buffer here..
55
			}
56
			fclose($storeHandle);
57
			return $publicKey;
58
		}
59
	}
60
61
	/**
62
	 * save a public key
63
	 * @param string $threemaId
64
	 * @param string $publicKey
65
	 * @return bool
66
	 */
67
	function savePublicKey($threemaId, $publicKey) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
68
		return file_put_contents($this->file, $threemaId.$publicKey."\n", FILE_APPEND) !== false;
69
	}
70
71
	/**
72
	 * Initialize a new File Public Key Store
73
	 * @param string $path the file will be created if it does not exist
74
	 * @return File
75
	 */
76
	public static function create($path) {
77
		if(false === file_exists($path)) {
78
			//touch
79
			touch($path);
80
		}
81
82
		return new File($path);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \Threema\MsgA...cKeyStores\File($path); (Threema\MsgApi\PublicKeyStores\File) is incompatible with the return type declared by the abstract method Threema\MsgApi\PublicKeyStore::create of type Threema\MsgApi\File|Threema\MsgApi\PhpFile.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
83
	}
84
}
85