Skip to content

Commit

Permalink
added php cs fixer config, updated composer.json
Browse files Browse the repository at this point in the history
  • Loading branch information
koertho committed Aug 1, 2018
1 parent 914f5b1 commit 32ab34d
Show file tree
Hide file tree
Showing 9 changed files with 135 additions and 128 deletions.
46 changes: 46 additions & 0 deletions .php_cs
@@ -0,0 +1,46 @@
<?php

$date = date('Y');

$header = <<<EOF
Copyright (c) $date Heimrich & Hannot GmbH
@license LGPL-3.0-or-later
EOF;

$finder = PhpCsFixer\Finder::create()
->exclude('Resources')
->exclude('Fixtures')
->in([__DIR__.'/src', __DIR__.'/tests'])
->exclude('vendor')
->exclude('docs')
->in([__DIR__])
;

return PhpCsFixer\Config::create()
->setRules([
'@Symfony' => true,
'@Symfony:risky' => true,
'psr0' => false,
'strict_comparison' => false,
'strict_param' => false,
'array_syntax' => ['syntax' => 'short'],
'heredoc_to_nowdoc' => true,
'header_comment' => ['header' => $header],
'ordered_imports' => true,
'ordered_class_elements' => true,
'php_unit_strict' => true,
'phpdoc_order' => true,
'no_useless_return' => true,
'no_useless_else' => true,
'no_unreachable_default_argument_value' => true,
'combine_consecutive_unsets' => true,
'general_phpdoc_annotation_remove' => [
'expectedException',
'expectedExceptionMessage',
],
])
->setFinder($finder)
->setRiskyAllowed(true)
->setUsingCache(false)
;
1 change: 1 addition & 0 deletions composer.json
Expand Up @@ -2,6 +2,7 @@
"name": "heimrichhannot/contao-inserttagcollection-bundle",
"description": "A collection of additional inserttags for contao cms.",
"type": "contao-bundle",
"homepage":"https://github.com/heimrichhannot/contao-inserttagcollection-bundle",
"authors":[
{
"name":"Heimrich & Hannot",
Expand Down
16 changes: 6 additions & 10 deletions src/DependencyInjection/InserttagCollectionExtension.php
@@ -1,35 +1,31 @@
<?php
/**
* Contao Open Source CMS
*

/*
* Copyright (c) 2018 Heimrich & Hannot GmbH
*
* @author Thomas Körner <t.koerner@heimrich-hannot.de>
* @license http://www.gnu.org/licences/lgpl-3.0.html LGPL
* @license LGPL-3.0-or-later
*/


namespace HeimrichHannot\ContaoInserttagCollectionBundle\DependencyInjection;


use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;

class InserttagCollectionExtension extends Extension
{

/**
* Loads a specific configuration.
*
* @param array $configs
* @param array $configs
* @param ContainerBuilder $container
*
* @throws \Exception
*/
public function load(array $configs, ContainerBuilder $container)
{
$loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yml');
}
}
}
118 changes: 53 additions & 65 deletions src/EventListener/InserttagListener.php
@@ -1,17 +1,13 @@
<?php
/**
* Contao Open Source CMS
*

/*
* Copyright (c) 2018 Heimrich & Hannot GmbH
*
* @author Thomas Körner <t.koerner@heimrich-hannot.de>
* @license http://www.gnu.org/licences/lgpl-3.0.html LGPL
* @license LGPL-3.0-or-later
*/


namespace HeimrichHannot\ContaoInserttagCollectionBundle\EventListener;


use Contao\ContentDownload;
use Contao\ContentModel;
use Contao\CoreBundle\Framework\ContaoFrameworkInterface;
Expand Down Expand Up @@ -42,12 +38,10 @@ public function replaceInserttags(string $tag)
{
$tag = trim($tag, '{}');
$tag = explode('::', $tag);
if (empty($tag) || count($tag) < 2)
{
if (empty($tag) || count($tag) < 2) {
return false;
}
switch ($tag[0])
{
switch ($tag[0]) {
case 'link_url_abs':
return $this->linkAbsoluteUrl($tag);
case 'email_label':
Expand All @@ -59,107 +53,101 @@ public function replaceInserttags(string $tag)
case 'download_size':
return $this->downloadSize($tag);
}
return false;

return false;
}

public function linkAbsoluteUrl(array $tag)
{
if (isset($tag[1]))
{
if (isset($tag[1])) {
$pageModel = $this->container->get('huh.utils.url')->getJumpToPageObject($tag[1]);
if ($pageModel)
{
if ($pageModel) {
$url = $this->container->get('contao.routing.url_generator')->generate($pageModel->alias, [], UrlGenerator::ABSOLUTE_URL);

return $url;
}
}

return '';
}

/**
* @param array $tag
* @return string Mailto-Link or empty string, if no valid mail adress given.
*
* @return string mailto-Link or empty string, if no valid mail adress given
*/
public function emailLabel(array $tag)
{
if (!isset($tag[1]) || !Validator::isEmail($tag[1]))
{
if (!isset($tag[1]) || !Validator::isEmail($tag[1])) {
return '';
}
$emailLabel = $tag[1];
$email = $this->framework->getAdapter(StringUtil::class)->encodeEmail('mailto:' . $tag[1]);
$email = $this->framework->getAdapter(StringUtil::class)->encodeEmail('mailto:'.$tag[1]);
// label parameters
$label = (isset($tag[2]) && !empty($tag[2])) ? $tag[2] : preg_replace('/\?.*$/', '', $emailLabel);
$classes = (isset($tag[3]) && !empty($tag[3])) ? ' class="' . $tag[3] . '"' : '';
$id = (isset($tag[4]) && !empty($tag[4])) ? 'id="' . $tag[4] . '" ' : '';

$label = (isset($tag[2]) && !empty($tag[2])) ? $tag[2] : preg_replace('/\?.*$/', '', $emailLabel);
$classes = (isset($tag[3]) && !empty($tag[3])) ? ' class="'.$tag[3].'"' : '';
$id = (isset($tag[4]) && !empty($tag[4])) ? 'id="'.$tag[4].'" ' : '';

$link = sprintf('<a %shref="%s"%s>%s</a>', $id, $email, $classes, $label);

return $link;
}

public function download(array $tag)
{
$download = $this->generateDownload($tag);

return $download->generate();
}

public function downloadLink(array $tag)
{
$download = $this->generateDownload($tag);
$download->generate();

return $download->Template->href;
}

public function downloadSize(array $tag)
{
$download = $this->generateDownload($tag);
$download->generate();

return $download->Template->filesize;
}

private function generateDownload(array $tag)
{
$source = strip_tags(($tag[1])); // remove <span> etc, otherwise Validator::isuuid fail

$file = null;
if (Validator::isUuid($source))
{
if (Validator::isUuid($source)) {
/** @var FilesModel $file */
$file = $this->framework->getAdapter(FilesModel::class)->findByUuid($source);
} elseif (($pos = strpos($source, '/')) !== false)
{
if (0 === $pos)
{
$source = ltrim($source, "/");
} elseif (false !== ($pos = strpos($source, '/'))) {
if (0 === $pos) {
$source = ltrim($source, '/');
}
/** @var FilesModel $file */
$file = $this->framework->getAdapter(FilesModel::class)->findByPath($source);
}
if ($file && $file->uuid)
{
if ($file && $file->uuid) {
$source = StringUtil::binToUuid($file->uuid);
}

$downloadData = $this->framework->createInstance(ContentModel::class);
$downloadData = $this->framework->createInstance(ContentModel::class);
$downloadData->customTpl = 'ce_download_inserttag';
$downloadData->singleSRC = $source;
if (isset($tag[2]) && is_string($tag[2]))
{
if (isset($tag[2]) && is_string($tag[2])) {
$downloadData->linkTitle = $tag[2];
}
if (isset($tag[3]) && is_string($tag[3]))
{
$downloadData->cssID[1] = 'inserttag_download ' . strip_tags($tag[3]);
if (isset($tag[3]) && is_string($tag[3])) {
$downloadData->cssID[1] = 'inserttag_download '.strip_tags($tag[3]);
}
if (isset($tag[4]) && is_string($tag[4]))
{
$downloadData->cssID[0] = strip_tags($tag[4]);
if (isset($tag[4]) && is_string($tag[4])) {
$downloadData->cssID[0] = strip_tags($tag[4]);
}
return $this->framework->createInstance(ContentDownload::class, [$downloadData]);
}

public function download(array $tag)
{
$download = $this->generateDownload($tag);
return $download->generate();
}

public function downloadLink(array $tag)
{
$download = $this->generateDownload($tag);
$download->generate();
return $download->Template->href;
}

public function downloadSize(array $tag)
{
$download = $this->generateDownload($tag);
$download->generate();
return $download->Template->filesize;
return $this->framework->createInstance(ContentDownload::class, [$downloadData]);
}


}
}
12 changes: 4 additions & 8 deletions src/HeimrichHannotContaoInserttagCollectionBundle.php
@@ -1,17 +1,13 @@
<?php
/**
* Contao Open Source CMS
*

/*
* Copyright (c) 2018 Heimrich & Hannot GmbH
*
* @author Thomas Körner <t.koerner@heimrich-hannot.de>
* @license http://www.gnu.org/licences/lgpl-3.0.html LGPL
* @license LGPL-3.0-or-later
*/


namespace HeimrichHannot\ContaoInserttagCollectionBundle;


use HeimrichHannot\ContaoInserttagCollectionBundle\DependencyInjection\InserttagCollectionExtension;
use Symfony\Component\HttpKernel\Bundle\Bundle;

Expand All @@ -21,4 +17,4 @@ public function getContainerExtension()
{
return new InserttagCollectionExtension();
}
}
}
12 changes: 4 additions & 8 deletions tests/ContaoManager/PluginTest.php
@@ -1,17 +1,13 @@
<?php
/**
* Contao Open Source CMS
*

/*
* Copyright (c) 2018 Heimrich & Hannot GmbH
*
* @author Thomas Körner <t.koerner@heimrich-hannot.de>
* @license http://www.gnu.org/licences/lgpl-3.0.html LGPL
* @license LGPL-3.0-or-later
*/


namespace HeimrichHannot\ContaoInserttagCollectionBundle\Test\Plugin;


use Contao\CoreBundle\ContaoCoreBundle;
use Contao\ManagerPlugin\Bundle\Config\BundleConfig;
use Contao\ManagerPlugin\Bundle\Parser\DelegatingParser;
Expand All @@ -38,4 +34,4 @@ public function testGetBundles()
$this->assertSame(HeimrichHannotContaoInserttagCollectionBundle::class, $bundles[0]->getName());
$this->assertSame([ContaoCoreBundle::class], $bundles[0]->getLoadAfter());
}
}
}
12 changes: 4 additions & 8 deletions tests/DependenyInjection/InserttagCollectionExtensionTest.php
@@ -1,17 +1,13 @@
<?php
/**
* Contao Open Source CMS
*

/*
* Copyright (c) 2018 Heimrich & Hannot GmbH
*
* @author Thomas Körner <t.koerner@heimrich-hannot.de>
* @license http://www.gnu.org/licences/lgpl-3.0.html LGPL
* @license LGPL-3.0-or-later
*/


namespace HeimrichHannot\ContaoInserttagCollectionBundle\Test\DependencyInjection;


use HeimrichHannot\ContaoInserttagCollectionBundle\DependencyInjection\InserttagCollectionExtension;
use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\ContainerBuilder;
Expand All @@ -38,4 +34,4 @@ public function testCanBeInstantiated()
$extension = new InserttagCollectionExtension();
$this->assertInstanceOf(InserttagCollectionExtension::class, $extension);
}
}
}

0 comments on commit 32ab34d

Please sign in to comment.