| Conditions | 1 |
| Paths | 1 |
| Total Lines | 76 |
| Code Lines | 1 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 92 | { |
||
| 93 | $this->logException( $e ); |
||
| 94 | } |
||
| 95 | } |
||
| 96 | |||
| 97 | |||
| 98 | /** |
||
| 99 | * Adds the necessary headers and the download content to the reponse object |
||
| 100 | * |
||
| 101 | * @param \Aimeos\MShop\Order\Item\Base\Product\Attribute\Iface $item Order product attribute item with file reference |
||
| 102 | */ |
||
| 103 | protected function addDownload( \Aimeos\MShop\Order\Item\Base\Product\Attribute\Iface $item ) |
||
| 104 | { |
||
| 105 | $fs = $this->context()->fs( 'fs-secure' ); |
||
| 106 | $response = $this->view()->response(); |
||
| 107 | $value = (string) $item->getValue(); |
||
| 108 | |||
| 109 | if( $fs->has( $value ) ) |
||
| 110 | { |
||
| 111 | $name = $item->getName(); |
||
| 112 | |||
| 113 | if( pathinfo( $name, PATHINFO_EXTENSION ) == null |
||
| 114 | && ( $ext = pathinfo( $value, PATHINFO_EXTENSION ) ) != null |
||
| 115 | ) { |
||
| 116 | $name .= '.' . $ext; |
||
|
|
|||
| 117 | } |
||
| 118 | |||
| 119 | $response->withHeader( 'Content-Description', 'File Transfer' ); |
||
| 120 | $response->withHeader( 'Content-Type', 'application/octet-stream' ); |
||
| 121 | $response->withHeader( 'Content-Disposition', 'attachment; filename="' . $name . '"' ); |
||
| 122 | $response->withHeader( 'Content-Length', (string) $fs->size( $value ) ); |
||
| 123 | $response->withHeader( 'Cache-Control', 'must-revalidate' ); |
||
| 124 | $response->withHeader( 'Pragma', 'private' ); |
||
| 125 | $response->withHeader( 'Expires', '0' ); |
||
| 126 | |||
| 127 | $response->withBody( $response->createStream( $fs->reads( $value ) ) ); |
||
| 128 | } |
||
| 129 | elseif( filter_var( $value, FILTER_VALIDATE_URL ) !== false ) |
||
| 130 | { |
||
| 131 | $response->withHeader( 'Location', $value ); |
||
| 132 | $response->withStatus( 303 ); |
||
| 133 | } |
||
| 134 | else |
||
| 135 | { |
||
| 136 | $response->withStatus( 404 ); |
||
| 137 | } |
||
| 138 | } |
||
| 139 | |||
| 140 | |||
| 141 | /** |
||
| 142 | * Checks if the customer is allowed to download the file |
||
| 143 | * |
||
| 144 | * @param string|null $id Unique order base product attribute ID referencing the download file |
||
| 145 | * @return bool True if download is allowed, false if not |
||
| 146 | */ |
||
| 147 | protected function checkAccess( string $id = null ) : bool |
||
| 148 | { |
||
| 149 | $context = $this->context(); |
||
| 150 | |||
| 151 | if( ( $customerId = $context->user() ) !== null && $id !== null ) |
||
| 152 | { |
||
| 153 | $manager = \Aimeos\MShop::create( $context, 'order/base' ); |
||
| 154 | |||
| 155 | $search = $manager->filter(); |
||
| 156 | $expr = array( |
||
| 157 | $search->compare( '==', 'order.base.customerid', $customerId ), |
||
| 158 | $search->compare( '==', 'order.base.product.attribute.id', $id ), |
||
| 159 | ); |
||
| 160 | $search->setConditions( $search->and( $expr ) ); |
||
| 161 | $search->slice( 0, 1 ); |
||
| 162 | |||
| 163 | if( !$manager->search( $search )->isEmpty() ) { |
||
| 164 | return true; |
||
| 165 | } |
||
| 166 | } |
||
| 167 | |||
| 168 | return false; |
||
| 220 |