Classe upload
Estou continuando meus estudos sobre oop e SOLID, e queria saber se minhas classes estão violando algum principio do SOLID.
Segue a tentativa:
Upload.class.php
<?php
class Upload {
private $fileName;
private $fileSize = 2.0;
private $fileType;
private $filePath;
private $token;
protected $userFile;
public function __construct(Token $token) {
$this->token= $token;
}
public function setFileSize($maxSize) {
$this->fileSize = (float)$maxSize * 1024 * 1024;
}
public function setFileType(Array $type) {
$this->fileType = $type;
}
public function setFilePath($path) {
$this->filePath = $path;
}
public function setUserFile($userFile) {
$this->userFile = $userFile;
}
public function getFileName() {
if (is_null($this->fileName))
$this->randomName();
return $this->fileName;
}
public function uploadFile() {
if (!in_array($this->getExtension(), $this->fileType))
throw new RuntimeException("Extensão de arquivo não permitido.");
else if ($this->fileSize < $_FILES[$this->userFile]['size'])
throw new RuntimeException("Tamanho de arquivo não permitido.");
else if (!move_uploaded_file($_FILES[$this->userFile]['tmp_name'], $this->filePath.$this->getFileName()))
throw new RuntimeException("Falha ao mover arquivo.");
}
private function randomName() {
$this->fileName = $this->token->generateToken().'.'.$this->getExtension();
}
private function getExtension() {
return pathinfo($_FILES[$this->userFile]['name'], PATHINFO_EXTENSION);
}
}
?>
Esses sets, seria ideal colocar tudo no construtor?
Token.php
<?php
interface Token {
public function generateToken();
}
?>
Bin2Hex.class.php
class Bin2Hex implements Token {
public function generateToken() {
return bin2hex(openssl_random_pseudo_bytes(32));
}
}
UploadImage.class.php
<?php
class UploadImage extends Upload {
private $height;
private $width;
public function setImageHeight($height) {
$this->height = $height;
}
public function setImageWidth($width) {
$this->width = $width;
}
public function uploadFile() {
list($height, $width) = getimagesize($_FILES[$this->userFile]['tmp_name']);
if ($this->height < $height) {
throw new RuntimeException("Altura da imagem não permitida.");
} else if ($this->width < $width) {
throw new RuntimeException("Largura da imagem não permitida.");
}
parent::uploadFile();
}
}
->A ideia aqui seria uma classe mais especifica para imagem...
E a maior dúvida é se nessa parte viola o LSP (herança).
index.php
$upload = new UploadImage(new Bin2Hex());
$upload->setUserFile('photo');
$upload->setFilePath('uploads/');
$upload->setFileType(array('jpg', 'png'));
$upload->setFileSize(1.0);
$upload->setImageHeight(800);
$upload->setImageWidth(600);
try {
$upload->uploadFile();
} catch (RuntimeException $e) {
echo $e->getMessage();
}Discussão (2)
Carregando comentários...