Skip to content

Commit

Permalink
Merge pull request #89 from matiosfree/download_images
Browse files Browse the repository at this point in the history
Download images
  • Loading branch information
tooyz authored Mar 1, 2021
2 parents 4574022 + 3a6cae6 commit 46e96c2
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 13 deletions.
9 changes: 8 additions & 1 deletion src/Components/Fields/AbstractFieldAccessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,23 @@

namespace MoySklad\Components\Fields;

use MoySklad\Entities\AbstractEntity;

/**
* Class for storing different fields
* Class AbstractFieldAccessor
* @package MoySklad\Components\Fields
*/
abstract class AbstractFieldAccessor implements \JsonSerializable {
protected $storage;
/**
* @var AbstractEntity
*/
protected $e;

public function __construct($fields)
public function __construct($fields, AbstractEntity &$entity = null)
{
$this->e = $entity;
$this->storage = new \stdClass();
$this->replace($fields);
}
Expand Down
6 changes: 3 additions & 3 deletions src/Components/Fields/EntityFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ public function replace($fields)
foreach ( $fields as $fieldName => $field ){
switch ( $fieldName ){
case "meta":
$this->storage->meta = new MetaField($field);
$this->storage->meta = new MetaField($field, $this->e);
break;
case "attributes":
$this->storage->attributes = new AttributeCollection($field);
$this->storage->attributes = new AttributeCollection($field, $this->e);
break;
case "image":
$this->storage->image = new ImageField($field);
$this->storage->image = new ImageField($field, $this->e);
break;
default:
$this->storage->{$fieldName} = $field;
Expand Down
4 changes: 2 additions & 2 deletions src/Components/Fields/EntityRelation.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
class EntityRelation extends AbstractFieldAccessor {
private $relatedByClass = null;

public function __construct($fields, $relatedByClass)
public function __construct($fields, $relatedByClass, AbstractEntity &$entity = null)
{
parent::__construct($fields);
parent::__construct($fields, $entity);
$this->relatedByClass = $relatedByClass;
}

Expand Down
8 changes: 6 additions & 2 deletions src/Components/Fields/ImageField.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,14 @@ public function getDownloadLink($size = 'normal'){
* @throws \Exception
*/
public function download($saveFile, $size = 'normal'){
if ( $link = $this->getDownloadLink($size) ){
if ($link = $this->getDownloadLink($size)) {
$filePath = fopen($saveFile,'w+');
$client = new Client();
$response = $client->get($link, [RequestOptions::SINK => $filePath]);

$response = $this->e->getSkladInstance()->getClient()->getRaw(
$link, [RequestOptions::SINK => $filePath]
);

return $response->getStatusCode();
}
throw new \Exception("Image does not have a $size size link. Try to refresh hosting entity");
Expand Down
14 changes: 14 additions & 0 deletions src/Components/Http/MoySkladHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,20 @@ public function delete($method, $payload = [], $options = null){
);
}

/**
* @param $link
* @param $options
* @return \Psr\Http\Message\ResponseInterface
*/
public function getRaw($link, $options) {
if (empty($options['headers']['Authorization'])) {
$options['headers']['Authorization'] = "Basic " . base64_encode($this->login . ':' . $this->password);
}

$client = new Client();
return $client->get($link, $options);
}

public function getLastRequest(){
return RequestLog::getLast();
}
Expand Down
10 changes: 5 additions & 5 deletions src/Entities/AbstractEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ public function __construct(MoySklad $skladInstance, $fields = [], ConstructionS
{
if ( !$specs ) $specs = ConstructionSpecs::create();
if ( is_array($fields) === false && is_object($fields) === false) $fields = [$fields];
$this->fields = new EntityFields($fields);
$this->links = new EntityLinker([]);
$this->relations = new EntityRelation([], static::class);
$this->fields = new EntityFields($fields, $this);
$this->links = new EntityLinker([], $this);
$this->relations = new EntityRelation([], static::class, $this);
$this->skladHashCode = $skladInstance->hashCode();
$this->processConstructionSpecs($specs);
}
Expand Down Expand Up @@ -156,8 +156,8 @@ public function fresh(Expand $expand = null){
* @return $this
*/
public function replaceFields(AbstractEntity $entity){
$this->fields = new EntityFields($entity->fields);
$this->relations = new EntityRelation($entity->relations, get_class($this));
$this->fields = new EntityFields($entity->fields, $this);
$this->relations = new EntityRelation($entity->relations, get_class($this), $this);
return $this;
}

Expand Down

0 comments on commit 46e96c2

Please sign in to comment.