/** * @param string $key * @param DateTime $date */ public function restoreByCopyingOldVersion($key, DateTime $date) { $versions = $this->getVersions($key); /** @var CMService_AwsS3Versioning_Response_Version $versionToRestore */ $versionToRestore = Functional\first($versions, function (CMService_AwsS3Versioning_Response_Version $version) use($key, $date) { $isExactKeyMatch = $key === $version->getKey(); $isModifiedBeforeOrAt = $date >= $version->getLastModified(); return $isExactKeyMatch && $isModifiedBeforeOrAt; }); $keepCurrentVersion = $versionToRestore && $versionToRestore->getIsLatest(); if (!$keepCurrentVersion) { $hasNoPriorVersion = !$versionToRestore; $restoreVersionIsDeleteMarker = $versionToRestore && null === $versionToRestore->getETag() && null === $versionToRestore->getSize(); if ($hasNoPriorVersion || $restoreVersionIsDeleteMarker) { $this->_client->deleteObject(array('Bucket' => $this->_bucket, 'Key' => $key)); } else { $this->_client->copyObject(array('Bucket' => $this->_bucket, 'CopySource' => urlencode($this->_bucket . '/' . $key) . '?versionId=' . $versionToRestore->getId(), 'Key' => $key)); } } }
public function testGetFormatterDateException() { $timezoneNameList = \Functional\reject(DateTimeZone::listIdentifiers(), function ($timeZoneName) { return IntlTimeZone::fromDateTimeZone(new DateTimeZone($timeZoneName)); }); if (empty($timezoneNameList)) { $this->markTestSkipped('No unsupported timezones'); } try { $timeZoneName = \Functional\first($timezoneNameList); $render = new CM_Frontend_Render(); $render->getFormatterDate(IntlDateFormatter::SHORT, IntlDateFormatter::SHORT, null, new DateTimeZone($timeZoneName)); $this->fail('Date formatter created with unsupported timezone'); } catch (CM_Exception $ex) { $this->assertSame('Cannot create date formatter', $ex->getMessage()); } }
/** * @param string $code * @return CM_Emoticon|null */ public static function findByCode($code) { $code = (string) $code; $dataList = static::getEmoticonData(); $data = \Functional\first($dataList, function ($data) use($code) { return false !== array_search($code, $data['codes'], true); }); if ($data) { return new static($data['name'], $data); } return null; }
/** * @param CM_Comparable $other * @return boolean */ public function equals(CM_Comparable $other = null) { if (empty($other)) { return false; } /** @var CM_File_Filesystem $other */ if (!$this->getAdapter()->equals($other->getAdapter())) { return false; } /** @var CM_File_Filesystem[] $bothSecondaryList */ $bothSecondaryList = array_merge($this->getSecondaryList(), $other->getSecondaryList()); foreach ($bothSecondaryList as $secondary) { foreach (array($this->getSecondaryList(), $other->getSecondaryList()) as $secondaryList) { $secondaryFound = Functional\first($secondaryList, function (CM_File_Filesystem $secondaryCompare) use($secondary) { return $secondaryCompare->equals($secondary); }); if (!$secondaryFound) { return false; } } } return true; }
/** * @param Closure $regularCode * @param Closure $errorCode * @return mixed * @throws CM_Exception */ protected function _runWithCatching(Closure $regularCode, Closure $errorCode) { try { return $regularCode(); } catch (CM_Exception $ex) { $config = self::_getConfig(); $exceptionsToCatch = $config->exceptionsToCatch; $catchPublicExceptions = !empty($config->catchPublicExceptions); $errorOptions = \Functional\first($exceptionsToCatch, function ($options, $exceptionClass) use($ex) { return is_a($ex, $exceptionClass); }); $catchException = null !== $errorOptions; if ($catchException && isset($errorOptions['log']) && true === $errorOptions['log']) { $logLevel = isset($errorOptions['level']) ? $errorOptions['level'] : null; if (null === $logLevel) { $logLevel = CM_Log_Logger::exceptionToLevel($ex); } $context = new CM_Log_Context(); $context->setUser($this->getViewer()); $context->setException($ex); $this->getServiceManager()->getLogger()->addMessage('Response processing error', $logLevel, $context); } if (!$catchException && ($catchPublicExceptions && $ex->isPublic())) { $errorOptions = []; $catchException = true; } if ($catchException) { return $errorCode($ex, $errorOptions); } throw $ex; } }
/** * @param AbstractContext $context * @return Override|null */ public function findByContext(AbstractContext $context) { return \Functional\first($this->_list, function (Override $override) use($context) { return $override->getContext()->equals($context); }); }
/** * @param CM_Log_Context $context * @param CM_Log_Handler_Layered $layered * @return CM_Log_Logger|\Mocka\AbstractClassTrait * @throws \Mocka\Exception */ protected function _getLoggerMock(CM_Log_Context $context, CM_Log_Handler_Layered $layered) { $loggerMock = $this->mockClass('CM_Log_Logger')->newInstance([$context, $layered]); $logExceptionsMock = $loggerMock->mockMethod('_logHandlersExceptions'); $logExceptionsMock->set(function (CM_Log_Record $record, array $exceptionList, CM_Log_Context $context) use($loggerMock) { if ($exception = \Functional\first($exceptionList, function (CM_Log_HandlingException $e) { return $e->getOriginalException() instanceof PHPUnit_Framework_Exception; })) { throw $exception; } $loggerMock->callOriginalMethod('_logHandlersExceptions', func_get_args()); }); return $loggerMock; }
/** * @return string * @throws CM_Exception */ public function getPathInitial() { $path = Functional\first($this->_pathList); if (null === $path) { throw new CM_Exception('No path available'); } return $path; }
/** * @param string $indexName * @return CM_Elasticsearch_Type_Abstract * @throws CM_Exception_Invalid */ public function findType($indexName) { return \Functional\first($this->getTypes(), function (CM_Elasticsearch_Type_Abstract $type) use($indexName) { return $type->getIndex()->getName() === $indexName; }); }
/** * @param callable $regularCode * @param callable $errorCode * @return mixed * @throws CM_Exception */ protected function _runWithCatching(Closure $regularCode, Closure $errorCode) { try { return $regularCode(); } catch (CM_Exception $ex) { $config = self::_getConfig(); $exceptionsToCatch = $config->exceptionsToCatch; $catchPublicExceptions = !empty($config->catchPublicExceptions); $errorOptions = \Functional\first($exceptionsToCatch, function ($options, $exceptionClass) use($ex) { return is_a($ex, $exceptionClass); }); $catchException = null !== $errorOptions; if ($catchException) { if (isset($errorOptions['log'])) { $formatter = new CM_ExceptionHandling_Formatter_Plain_Log(); /** @var CM_Paging_Log_Abstract $log */ $log = new $errorOptions['log'](); $log->add($formatter->formatException($ex), $ex->getMetaInfo()); } } if (!$catchException && ($catchPublicExceptions && $ex->isPublic())) { $errorOptions = []; $catchException = true; } if ($catchException) { return $errorCode($ex, $errorOptions); } throw $ex; } }