Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Import products, Update product page #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions app/code/Cadence/Movie/Console/Command/MovieImport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace Cadence\Movie\Console\Command;

use Magento\Framework\App\Area;
use Magento\Framework\App\State;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Cadence\Movie\Service\MovieImport as MovieImportService;

class MovieImport extends \Symfony\Component\Console\Command\Command
{
/**
* @var MovieImportService
*/
private $movieImportService;

public function __construct(
MovieImportService $movieImportService,
State $state
) {
parent::__construct();

$this->movieImportService = $movieImportService;
$this->state = $state;
}

protected function configure()
{
$this->setName('cadence:movie:import');
$this->setDescription('Movie import');
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$output->writeln("Start Products Import.");
$this->state->setAreaCode(Area::AREA_ADMINHTML);
try {
$this->movieImportService->execute();
$output->writeln("Finish products Import.");
return \Magento\Framework\Console\Cli::RETURN_SUCCESS;
} catch (\Exception $exception) {
$output->writeln("<error>{$exception->getMessage()}</error>");
return \Magento\Framework\Console\Cli::RETURN_FAILURE;
}
}
}
8 changes: 0 additions & 8 deletions app/code/Cadence/Movie/Helper/Config.php

This file was deleted.

54 changes: 54 additions & 0 deletions app/code/Cadence/Movie/Model/Config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

declare(strict_types=1);

namespace Cadence\Movie\Model;

use Magento\Framework\App\Config\ScopeConfigInterface;

class Config
{
const MOVIE_CATEGORY_NAME = 'Movie';
const MOVIE_ATTRIBUTE_SET_NAME = 'Movie';
const MOVIE_PRODUCT_PRICE = 5.99;
const MOVIE_PRODUCT_QTY = 100;

const XML_PATH_TMDB_API_KEY = 'tmdb/api/key';
const XML_PATH_TMDB_API_REQUEST_URI = 'tmdb/api/request_uri';
const XML_PATH_TMDB_API_IMAGE_BASE_URI = 'tmdb/api/image_base_uri';

/**
* @var ScopeConfigInterface
*/
protected $scopeConfig;

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

public function getTmdbKey(): string
{
return $this->scopeConfig->getValue(
self::XML_PATH_TMDB_API_KEY,
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
);
}

public function getTmdbRequestUri(): string
{
return $this->scopeConfig->getValue(
self::XML_PATH_TMDB_API_REQUEST_URI,
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
);
}

public function getTmdbImageBaseUri(): string
{
return $this->scopeConfig->getValue(
self::XML_PATH_TMDB_API_IMAGE_BASE_URI,
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
);
}
}
53 changes: 53 additions & 0 deletions app/code/Cadence/Movie/Service/ImageImport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare(strict_types=1);

namespace Cadence\Movie\Service;

use Cadence\Movie\Model\Config;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Filesystem\Io\File;

class ImageImport
{
const MOVIE_IMAGE_FILE_SIZE = 'w1280';
const MOVIE_IMAGE_FILE_SIZE_THUMB = 'w500';

/**
* @var DirectoryList
*/
private $directoryList;

/**
* @var File
*/
private $file;

/**
* @var Config
*/
private $cadenceConfig;

public function __construct(
DirectoryList $directoryList,
File $file,
Config $cadenceConfig
) {
$this->directoryList = $directoryList;
$this->file = $file;
$this->cadenceConfig = $cadenceConfig;
}

public function execute(\Magento\Catalog\Model\Product $product, string $filePath)
{
$tmpDir = $this->directoryList->getPath(DirectoryList::MEDIA) . DIRECTORY_SEPARATOR . 'tmp';
$this->file->checkAndCreateFolder($tmpDir);
$imageUrl = $this->cadenceConfig->getTmdbImageBaseUri() . self::MOVIE_IMAGE_FILE_SIZE . $filePath;
$newFileName = $tmpDir . baseName($imageUrl);
$result = $this->file->read($imageUrl, $newFileName);
if ($result) {
$product->addImageToMediaGallery($newFileName, ['image', 'small_image', 'thumbnail'], false, false);
}
return $result;
}
}
Loading