path = $storePath; $this->state = 0; $this->dateAdd = new \DateTime(); $this->generateGuid(); } /** * @ORM\PrePersist * @ORM\PreUpdate */ public function prePersist(): void{ $this->createPath(); $this->updateFile(); } /** * create path to work with */ protected function createPath(): void{ if(empty($this->path)) throw new Exception('path cannot be empty'); if(!str_contains('.lock',$this->path)){ if(substr($this->path, -1) != "/") $this->path .= "/"; $this->path .= $this->guid.'.lock'; } } /** * put params in given file */ protected function updateFile(): void{ if(empty($this->params)) throw new Exception('param cannot be empty'); if(!is_file($this->path)){ $tmp = array(); $tmp['guid'] = $this->guid; $tmp['params'] = $this->params; $tmp['url'] = $this->returnUrl; if(!is_null($this->userFileOwnerInterface) && method_exists($this->userFileOwnerInterface,"getId")) $tmp['user'] = $this->userFileOwnerInterface->getId(); file_put_contents($this->path,json_encode($tmp)); } } /** * @param int $length * @return bool|string */ private function generateHumanHash($length = 64){ $hash = ""; while(strlen($hash) < $length){ $hash .= hash('sha512', random_bytes($length)); } return substr($hash, 0,$length); } /** * generate and return a guid for the current request * * @return string */ public function generateGuid(){ $md5 = $this->generateHumanHash(64); $newFileName = time().'P'.$md5; $this->guid = $newFileName; return $newFileName; } /** * @param UserFileOwnerInterface $userFileOwnerInterface * @return FileProcessItem */ public function setUserFileOwnerInterface(UserFileOwnerInterface $userFileOwnerInterface): FileProcessItem { $this->userFileOwnerInterface = $userFileOwnerInterface; return $this; } /** * @return UserFileOwnerInterface|null */ public function getUserFileOwnerInterface(): ?UserFileOwnerInterface { return $this->userFileOwnerInterface; } /** * Get id * * @return int */ public function getId() { return $this->id; } /** * Set type * * @param string $type * * @return FileProcessItem */ public function setType($type) { $this->type = str_replace(array('/','\\','@','..'),'',$type); return $this; } /** * Get type * * @return string */ public function getType() { return $this->type; } /** * Set state * * @param integer $state * * @return FileProcessItem */ public function setState($state) { $this->state = $state; return $this; } /** * Get state * * @return int */ public function getState() { return $this->state; } /** * Get dateAdd * * @return \DateTime */ public function getDateAdd() { return $this->dateAdd; } /** * Set guid * * @param string $guid * * @return FileProcessItem */ public function setGuid($guid) { $this->guid = $guid; return $this; } /** * Get guid * * @return string */ public function getGuid() { return $this->guid; } /** * @return string */ public function getPath(): string { return $this->path; } /** * @param string $path * @return FileProcessItem */ public function setPath(string $path): FileProcessItem { $this->path = $path; return $this; } /** * @return array */ public function getParams(): array { return $this->params; } /** * @param array $params * @return FileProcessItem */ public function setParams(array $params): FileProcessItem { $this->params = $params; return $this; } /** * @return string */ public function getResponse(): ?string { return $this->response; } /** * @param string $response * @return FileProcessItem */ public function setResponse(string $response): FileProcessItem { $this->response = $response; return $this; } /** * @return string */ public function getReturnUrl(): ?string { return $this->returnUrl; } /** * @param string $returnUrl * @return FileProcessItem */ public function setReturnUrl(string $returnUrl): FileProcessItem { $this->returnUrl = $returnUrl; return $this; } public function setDateAdd(\DateTimeInterface $dateAdd): self { $this->dateAdd = $dateAdd; return $this; } }