| Total Complexity | 17 |
| Total Lines | 142 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 36 | class FileHashTask extends Task |
||
| 37 | { |
||
| 38 | /** |
||
| 39 | * Property for File |
||
| 40 | * |
||
| 41 | * @var File file |
||
| 42 | */ |
||
| 43 | private $file; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Property to be set |
||
| 47 | * |
||
| 48 | * @var string $property |
||
| 49 | */ |
||
| 50 | private $propertyName = "filehashvalue"; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Specify which hash algorithm to use. |
||
| 54 | * 0 = MD5 |
||
| 55 | * 1 = SHA1 |
||
| 56 | * |
||
| 57 | * @var integer $hashtype |
||
| 58 | */ |
||
| 59 | private $hashtype = 0; |
||
| 60 | |||
| 61 | /** @var string $algorithm */ |
||
| 62 | private $algorithm = ''; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Specify if MD5 or SHA1 hash should be used |
||
| 66 | * |
||
| 67 | * @param integer $type 0=MD5, 1=SHA1 |
||
| 68 | */ |
||
| 69 | public function setHashtype($type): void |
||
| 70 | { |
||
| 71 | $this->hashtype = $type; |
||
| 72 | } |
||
| 73 | |||
| 74 | public function setAlgorithm($type): void |
||
| 75 | { |
||
| 76 | $this->algorithm = strtolower($type); |
||
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Which file to calculate the hash value of |
||
| 81 | * |
||
| 82 | * @param File $file |
||
| 83 | */ |
||
| 84 | public function setFile($file): void |
||
| 85 | { |
||
| 86 | $this->file = $file; |
||
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Set the name of the property to store the hash value in |
||
| 91 | * |
||
| 92 | * @param $property |
||
| 93 | * @return void |
||
| 94 | */ |
||
| 95 | public function setPropertyName($property): void |
||
| 96 | { |
||
| 97 | $this->propertyName = $property; |
||
| 98 | } |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Main-Method for the Task |
||
| 102 | * |
||
| 103 | * @throws BuildException |
||
| 104 | */ |
||
| 105 | public function main() |
||
| 106 | { |
||
| 107 | $this->checkFile(); |
||
| 108 | $this->checkPropertyName(); |
||
| 109 | |||
| 110 | // read file |
||
| 111 | if ($this->algorithm !== '' && in_array($this->algorithm, hash_algos())) { |
||
| 112 | $this->log("Calculating $this->algorithm hash from: " . $this->file); |
||
| 113 | $hashValue = hash_file($this->algorithm, $this->file); |
||
| 114 | } elseif ((int)$this->hashtype === 0) { |
||
| 115 | $this->log("Calculating MD5 hash from: " . $this->file); |
||
| 116 | $hashValue = md5_file($this->file, false); |
||
| 117 | $this->algorithm = 'md5'; |
||
| 118 | } elseif ((int)$this->hashtype === 1) { |
||
| 119 | $this->log("Calculating SHA1 hash from: " . $this->file); |
||
| 120 | $hashValue = sha1_file($this->file, false); |
||
| 121 | $this->algorithm = 'sha1'; |
||
| 122 | } else { |
||
| 123 | if ($this->algorithm !== '') { |
||
| 124 | throw new BuildException( |
||
| 125 | sprintf( |
||
| 126 | '[FileHash] Unknown algorithm specified %d. Must be one of %s', |
||
| 127 | $this->algorithm, |
||
| 128 | implode(', ', hash_algos()) |
||
| 129 | ) |
||
| 130 | ); |
||
| 131 | } |
||
| 132 | |||
| 133 | throw new BuildException( |
||
| 134 | sprintf( |
||
| 135 | '[FileHash] Unknown hashtype specified %d. Must be either 0 (=MD5) or 1 (=SHA1)', |
||
| 136 | $this->hashtype |
||
| 137 | ) |
||
| 138 | ); |
||
| 139 | } |
||
| 140 | |||
| 141 | // publish hash value |
||
| 142 | $this->project->setProperty($this->propertyName, $hashValue); |
||
| 143 | $fos = new FileOutputStream($this->file . '.' . $this->algorithm); |
||
| 144 | $fos->write(sprintf('%s %s', $hashValue, basename($this->file))); |
||
| 145 | } |
||
| 146 | |||
| 147 | /** |
||
| 148 | * checks file attribute |
||
| 149 | * |
||
| 150 | * @throws BuildException |
||
| 151 | */ |
||
| 152 | private function checkFile(): void |
||
| 164 | ) |
||
| 165 | ); |
||
| 166 | } |
||
| 167 | } |
||
| 168 | |||
| 169 | /** |
||
| 170 | * checks property attribute |
||
| 171 | * |
||
| 172 | * @throws BuildException |
||
| 173 | */ |
||
| 174 | private function checkPropertyName(): void |
||
| 178 | } |
||
| 179 | } |
||
| 180 | } |
||
| 181 |