Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Placeholder.php
Go to the documentation of this file.
1 <?php
8 
14 
18 class Placeholder implements LocalInterface
19 {
25  private $type;
26 
32  private $filePath;
33 
37  private $contentType = 'image';
38 
42  private $context;
43 
47  private $assetRepo;
48 
54  private $scopeConfig;
55 
64  public function __construct(
65  ContextInterface $context,
66  ScopeConfigInterface $scopeConfig,
67  Repository $assetRepo,
68  $type
69  ) {
70  $this->context = $context;
71  $this->scopeConfig = $scopeConfig;
72  $this->assetRepo = $assetRepo;
73  $this->type = $type;
74  }
75 
79  public function getUrl()
80  {
81  if ($this->getFilePath() !== null) {
82  $result = $this->context->getBaseUrl() . '/' . $this->getModule() . '/' . $this->getFilePath();
83  } else {
84  $result = $this->assetRepo->getUrl("Magento_Catalog::images/product/placeholder/{$this->type}.jpg");
85  }
86 
87  return $result;
88  }
89 
93  public function getContentType()
94  {
95  return $this->contentType;
96  }
97 
101  public function getPath()
102  {
103  if ($this->getFilePath() !== null) {
104  $result = $this->getContext()->getPath()
105  . DIRECTORY_SEPARATOR . $this->getModule()
106  . DIRECTORY_SEPARATOR . $this->getFilePath();
107  } else {
108  $defaultPlaceholder = $this->assetRepo->createAsset(
109  "Magento_Catalog::images/product/placeholder/{$this->type}.jpg"
110  );
111  try {
112  $result = $defaultPlaceholder->getSourceFile();
113  } catch (NotFoundException $e) {
114  $result = null;
115  }
116  }
117 
118  return $result;
119  }
120 
124  public function getSourceFile()
125  {
126  return $this->getPath();
127  }
128 
134  public function getSourceContentType()
135  {
136  return $this->contentType;
137  }
138 
142  public function getContent()
143  {
144  return null;
145  }
146 
150  public function getFilePath()
151  {
152  if ($this->filePath !== null) {
153  return $this->filePath;
154  }
155  // check if placeholder defined in config
156  $isConfigPlaceholder = $this->scopeConfig->getValue(
157  "catalog/placeholder/{$this->type}_placeholder",
158  \Magento\Store\Model\ScopeInterface::SCOPE_STORE
159  );
160  $this->filePath = $isConfigPlaceholder;
161 
162  return $this->filePath;
163  }
164 
169  public function getContext()
170  {
171  return $this->context;
172  }
173 
177  public function getModule()
178  {
179  return 'placeholder';
180  }
181 }
__construct(ContextInterface $context, ScopeConfigInterface $scopeConfig, Repository $assetRepo, $type)
Definition: Placeholder.php:64