Skip to content

Commit

Permalink
Merge pull request #10 from sgrodzicki/master
Browse files Browse the repository at this point in the history
Fixed PHPDoc & PSR Coding Standards
  • Loading branch information
tedivm committed Nov 8, 2012
2 parents baaae55 + 755d50a commit b7c53f6
Show file tree
Hide file tree
Showing 3 changed files with 1,112 additions and 1,123 deletions.
331 changes: 166 additions & 165 deletions src/Fetch/Attachment.php
Expand Up @@ -21,180 +21,181 @@
class Attachment
{

/**
* This is the structure object for the piece of the message body that the attachment is located it.
*
* @var stdClass
*/
protected $structure;

/**
* This is the unique identifier for the message this attachment belongs to.
*
* @var unknown_type
*/
protected $messageId;

/**
* This is the ImapResource.
*
* @var resource
*/
protected $imapStream;

/**
* This is the id pointing to the section of the message body that contains the attachment.
*
* @var unknown_type
*/
protected $partId;

/**
* This is the attachments filename.
*
* @var unknown_type
*/
protected $filename;

/**
* This is the size of the attachment.
*
* @var int
*/
protected $size;

/**
* This stores the data of the attachment so it doesn't have to be retrieved from the server multiple times. It is
* only populated if the getData() function is called and should not be directly used.
*
* @internal
* @var unknown_type
*/
protected $data;

/**
* This function takes in an ImapMessage, the structure object for the particular piece of the message body that the
* attachment is located at, and the identifier for that body part. As a general rule you should not be creating
* instances of this yourself, but rather should get them from an ImapMessage class.
*
* @param ImapMessage $message
* @param stdClass $structure
* @param string $partIdentifier
*/
public function __construct(Message $message, $structure, $partIdentifier = null)
{
$this->messageId = $message->getUid();
$this->imapStream = $message->getImapBox()->getImapStream();
$this->structure = $structure;

if(isset($partIdentifier))
$this->partId = $partIdentifier;

$parameters = Message::getParametersFromStructure($structure);

if(isset($parameters['filename']))
{
$this->filename = $parameters['filename'];
}elseif(isset($parameters['name'])){
$this->filename = $parameters['name'];
}

$this->size = $structure->bytes;

$this->mimeType = Message::typeIdToString($structure->type);

if(isset($structure->subtype))
$this->mimeType .= '/' . strtolower($structure->subtype);

$this->encoding = $structure->encoding;
/**
* This is the structure object for the piece of the message body that the attachment is located it.
*
* @var \stdClass
*/
protected $structure;

/**
* This is the unique identifier for the message this attachment belongs to.
*
* @var int
*/
protected $messageId;

/**
* This is the ImapResource.
*
* @var resource
*/
protected $imapStream;

/**
* This is the id pointing to the section of the message body that contains the attachment.
*
* @var int
*/
protected $partId;

/**
* This is the attachments filename.
*
* @var string
*/
protected $filename;

/**
* This is the size of the attachment.
*
* @var int
*/
protected $size;

/**
* This stores the data of the attachment so it doesn't have to be retrieved from the server multiple times. It is
* only populated if the getData() function is called and should not be directly used.
*
* @internal
* @var array
*/
protected $data;

/**
* This function takes in an ImapMessage, the structure object for the particular piece of the message body that the
* attachment is located at, and the identifier for that body part. As a general rule you should not be creating
* instances of this yourself, but rather should get them from an ImapMessage class.
*
* @param Message $message
* @param \stdClass $structure
* @param string $partIdentifier
*/
public function __construct(Message $message, $structure, $partIdentifier = null)
{
$this->messageId = $message->getUid();
$this->imapStream = $message->getImapBox()->getImapStream();
$this->structure = $structure;

if (isset($partIdentifier))
$this->partId = $partIdentifier;

$parameters = Message::getParametersFromStructure($structure);

if (isset($parameters['filename'])) {
$this->filename = $parameters['filename'];
} elseif (isset($parameters['name'])) {
$this->filename = $parameters['name'];
}

/**
* This function returns the data of the attachment. Combined with getMimeType() it can be used to directly output
* data to a browser.
*
* @return binary
*/
public function getData()
{
if(!isset($this->data))
{
$messageBody = isset($this->partId) ?
imap_fetchbody($this->imapStream, $this->messageId, $this->partId, FT_UID)
: imap_body($this->imapStream, $this->messageId, FT_UID);

$messageBody = Message::decode($messageBody, $this->encoding);
$this->data = $messageBody;
}
return $this->data;
}
$this->size = $structure->bytes;

/**
* This returns the filename of the attachment, or false if one isn't given.
*
* @return string
*/
public function getFileName()
{
return (isset($this->filename)) ? $this->filename : false;
}
$this->mimeType = Message::typeIdToString($structure->type);

/**
* This function returns the mimetype of the attachment.
*
* @return string
*/
public function getMimeType()
{
return $this->mimeType;
}
if (isset($structure->subtype))
$this->mimeType .= '/' . strtolower($structure->subtype);

/**
* This returns the size of the attachment.
*
* @return int
*/
public function getSize()
{
return $this->size;
}
$this->encoding = $structure->encoding;
}

/**
* This function saves the attachment to the passed directory, keeping the original name of the file.
*
* @param string $path
*/
public function saveToDirectory($path)
{
$path = rtrim($path, '/') . '/';
/**
* This function returns the data of the attachment. Combined with getMimeType() it can be used to directly output
* data to a browser.
*
* @return string
*/
public function getData()
{
if (!isset($this->data)) {
$messageBody = isset($this->partId) ?
imap_fetchbody($this->imapStream, $this->messageId, $this->partId, FT_UID)
: imap_body($this->imapStream, $this->messageId, FT_UID);

if(is_dir($path))
return $this->saveAs($path . $this->getFileName());
$messageBody = Message::decode($messageBody, $this->encoding);
$this->data = $messageBody;
}

return $this->data;
}

/**
* This returns the filename of the attachment, or false if one isn't given.
*
* @return string
*/
public function getFileName()
{
return (isset($this->filename)) ? $this->filename : false;
}

/**
* This function returns the mimetype of the attachment.
*
* @return string
*/
public function getMimeType()
{
return $this->mimeType;
}

/**
* This returns the size of the attachment.
*
* @return int
*/
public function getSize()
{
return $this->size;
}

/**
* This function saves the attachment to the passed directory, keeping the original name of the file.
*
* @param string $path
* @return bool
*/
public function saveToDirectory($path)
{
$path = rtrim($path, '/') . '/';

if (is_dir($path))
return $this->saveAs($path . $this->getFileName());

return false;
}

/**
* This function saves the attachment to the exact specified location.
*
* @param string $path
* @return bool
*/
public function saveAs($path)
{
$dirname = dirname($path);
if (file_exists($path)) {
if (!is_writable($path))
return false;
} elseif (!is_dir($dirname) || !is_writable($dirname)) {
return false;
}

/**
* This function saves the attachment to the exact specified location.
*
* @param path $path
*/
public function saveAs($path)
{
$dirname = dirname($path);
if(file_exists($path))
{
if(!is_writable($path))
return false;
}elseif(!is_dir($dirname) || !is_writable($dirname)){
return false;
}

if(($filePointer = fopen($path, 'w')) == false)
return false;

$results = fwrite($filePointer, $this->getData());
fclose($filePointer);
return is_numeric($results);
}
if (($filePointer = fopen($path, 'w')) == false)
return false;

$results = fwrite($filePointer, $this->getData());
fclose($filePointer);

return is_numeric($results);
}
}

0 comments on commit b7c53f6

Please sign in to comment.