/** * Override function to generate less curly captcha that will not cut off * * @see \Zend_Captcha_Image::_randomSize() * @return int */ protected function _randomSize() { return \Magento\Framework\Math\Random::getRandomNumber(280, 300) / 100; }
/** * Calculate cleanup possibility for data with lifetime property * * @return bool */ public function isCleanupProbability() { // Safe get cleanup probability value from system configuration $configValue = (int) $this->_scopeConfig->getValue(self::XML_PATH_CLEANUP_PROBABILITY, \Magento\Store\Model\ScopeInterface::SCOPE_STORE); return $configValue > 0 ? 1 == \Magento\Framework\Math\Random::getRandomNumber(1, $configValue) : false; }
/** * Create \Zend_Pdf_Page instance with image from $imageString. Supports JPEG, PNG, GIF, WBMP, and GD2 formats. * * @param string $imageString * @return \Zend_Pdf_Page|false */ public function createPdfPageFromImageString($imageString) { /** @var \Magento\Framework\Filesystem\Directory\Write $directory */ $directory = $this->filesystem->getDirectoryWrite(DirectoryList::TMP); $directory->create(); $image = @imagecreatefromstring($imageString); if (!$image) { return false; } $xSize = imagesx($image); $ySize = imagesy($image); $page = new \Zend_Pdf_Page($xSize, $ySize); imageinterlace($image, 0); $tmpFileName = $directory->getAbsolutePath('shipping_labels_' . uniqid(\Magento\Framework\Math\Random::getRandomNumber()) . time() . '.png'); imagepng($image, $tmpFileName); $pdfImage = \Zend_Pdf_Image::imageWithPath($tmpFileName); $page->drawImage($pdfImage, 0, 0, $xSize, $ySize); $directory->delete($directory->getRelativePath($tmpFileName)); return $page; }
/** * Processing object before save data * * @return $this */ protected function _beforeSave() { parent::_beforeSave(); $this->_checkState(); if (!$this->getId()) { $store = $this->getStore(); $name = array($store->getWebsite()->getName(), $store->getGroup()->getName(), $store->getName()); $this->setStoreName(implode("\n", $name)); } if (!$this->getIncrementId()) { $incrementId = $this->_eavConfig->getEntityType('order')->fetchNewIncrementId($this->getStoreId()); $this->setIncrementId($incrementId); } /** * Process items dependency for new order */ if (!$this->getId()) { $itemsCount = 0; foreach ($this->getAllItems() as $item) { $parent = $item->getQuoteParentItemId(); if ($parent && !$item->getParentItem()) { $item->setParentItem($this->getItemByQuoteItemId($parent)); } elseif (!$parent) { $itemsCount++; } } // Set items count $this->setTotalItemCount($itemsCount); } /** TODO refactor getCustomer usage after MAGETWO-20182 and MAGETWO-20258 are done */ $isNewCustomer = !$this->getCustomerId() || $this->getCustomerId() === true; if ($isNewCustomer && $this->getCustomer()) { $this->setCustomerId($this->getCustomer()->getId()); } if ($this->hasBillingAddressId() && $this->getBillingAddressId() === null) { $this->unsBillingAddressId(); } if ($this->hasShippingAddressId() && $this->getShippingAddressId() === null) { $this->unsShippingAddressId(); } $this->setData('protect_code', substr(md5(uniqid(\Magento\Framework\Math\Random::getRandomNumber(), true) . ':' . microtime(true)), 5, 6)); return $this; }
/** * @param \Magento\Framework\Model\AbstractModel $object * @return $this */ protected function _beforeSave(\Magento\Framework\Model\AbstractModel $object) { if (!$object->getId()) { /** @var \Magento\Store\Model\Store $store */ $store = $object->getStore(); $name = [$store->getWebsite()->getName(), $store->getGroup()->getName(), $store->getName()]; $object->setStoreName(implode(PHP_EOL, $name)); $object->setTotalItemCount($this->calculateItems($object)); } $object->setData('protect_code', substr(md5(uniqid(Random::getRandomNumber(), true) . ':' . microtime(true)), 5, 6)); $isNewCustomer = !$object->getCustomerId() || $object->getCustomerId() === true; if ($isNewCustomer && $object->getCustomer()) { $object->setCustomerId($object->getCustomer()->getId()); } return parent::_beforeSave($object); }
/** * Generate coupon code * * @return string */ public function generateCode() { $format = $this->getFormat(); if (!$format) { $format = \Magento\SalesRule\Helper\Coupon::COUPON_FORMAT_ALPHANUMERIC; } $length = max(1, (int) $this->getLength()); $split = max(0, (int) $this->getDash()); $suffix = $this->getSuffix(); $prefix = $this->getPrefix(); $splitChar = $this->getDelimiter(); $charset = $this->_salesRuleCoupon->getCharset($format); $code = ''; $charsetSize = count($charset); for ($i = 0; $i < $length; $i++) { $char = $charset[\Magento\Framework\Math\Random::getRandomNumber(0, $charsetSize - 1)]; if ($split > 0 && $i % $split == 0 && $i != 0) { $char = $splitChar . $char; } $code .= $char; } $code = $prefix . $code . $suffix; return $code; }
/** * Goes to specified host/path and fetches reports from there. * Save reports to database. * * @param \Magento\Framework\Filesystem\Io\Sftp $connection * @return int Number of report rows that were fetched and saved successfully * @throws \Magento\Framework\Exception\LocalizedException */ public function fetchAndSave(\Magento\Framework\Filesystem\Io\Sftp $connection) { $fetched = 0; $listing = $this->_filterReportsList($connection->rawls()); foreach ($listing as $filename => $attributes) { $localCsv = 'PayPal_STL_' . uniqid(\Magento\Framework\Math\Random::getRandomNumber()) . time() . '.csv'; if ($connection->read($filename, $this->_tmpDirectory->getAbsolutePath($localCsv))) { if (!$this->_tmpDirectory->isWritable($localCsv)) { throw new \Magento\Framework\Exception\LocalizedException(__('We cannot create a target file for reading reports.')); } $encoded = $this->_tmpDirectory->readFile($localCsv); $csvFormat = 'new'; $fileEncoding = mb_detect_encoding($encoded); if (self::FILES_OUT_CHARSET != $fileEncoding) { $decoded = @iconv($fileEncoding, self::FILES_OUT_CHARSET . '//IGNORE', $encoded); $this->_tmpDirectory->writeFile($localCsv, $decoded); $csvFormat = 'old'; } // Set last modified date, this value will be overwritten during parsing if (isset($attributes['mtime'])) { $lastModified = new \DateTime($attributes['mtime']); $this->setReportLastModified($lastModified->format('Y-m-d H:i:s')); } $this->setReportDate($this->_fileNameToDate($filename))->setFilename($filename)->parseCsv($localCsv, $csvFormat); if ($this->getAccountId()) { $this->save(); } if ($this->_dataSaveAllowed) { $fetched += count($this->_rows); } // clean object and remove parsed file $this->unsetData(); $this->_tmpDirectory->delete($localCsv); } } return $fetched; }
/** * @param $min * @param $max * * @dataProvider testGetRandomNumberProvider */ public function testGetRandomNumber($min, $max) { $number = \Magento\Framework\Math\Random::getRandomNumber($min, $max); $this->assertLessThanOrEqual($max, $number); $this->assertGreaterThanOrEqual($min, $number); }
/** * Returns string of random chars * * @param int $length * @return string */ public function randomSequence($length = 32) { $id = ''; $par = []; $char = array_merge(range('a', 'z'), range(0, 9)); $charLen = count($char) - 1; for ($i = 0; $i < $length; $i++) { $disc = \Magento\Framework\Math\Random::getRandomNumber(0, $charLen); $par[$i] = $char[$disc]; $id = $id . $char[$disc]; } return $id; }
/** * Return unique string based on the time in microseconds. * * @return string */ public function generateUniqueIdPath() { return str_replace('.', '_', uniqid(\Magento\Framework\Math\Random::getRandomNumber(), true)); }